Android新手入門2016(14)--FragmentTabHost實現選項卡和菜單


本文來自肥寶傳說之路,引用必須注明出處!

這章憋了好久。本來想寫選項卡的,學到TabHost,TabWidget的,把代碼拿過來准備研究的時候,發現竟然在4.0.3版本號被廢棄了。

百度一下,發如今后面的版本號,用FragmentTabHost和LayoutInflater來取代了。網上也有一些關於Frame的內容,可是都不是新手教程的。

寫得不夠通俗。想直接拿代碼下來研究,發現竟然非常多人都是上傳代碼片段,然后再給個收費鏈接。作為一個窮屌絲,僅僅能自己一點一點去研究了。

Frament是什么?直譯是片段的意思,Frament是為了解決Android同一套代碼在不同尺寸屏幕的設備上顯示的問題而誕生的,是Activity的一個部分。

事實上就是把界面分成一部分一部分的方便管理。更深入的了解能夠看這里點擊打開鏈接

FragmentActivity 繼承自Activity。可以使用Frament相關內容的Activity。


FragmentTabHost 替代了TabHost的類

來,先看個圖:



呃。。

。MacBook的圖就是打。

。。

邊看代碼邊說吧:

activity_hello_world.xml 主界面的布局文件,使用了幀布局,將菜單條和頁面發在一起,同一時候顯示

<?

xml version="1.0" encoding="utf-8"?

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/realtabcontent" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/bottom_bar"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>


我們能夠看出,菜單條須要一個布局文件來控制這些button的擺放。同一時候,選項卡頁面內,也是須要有一個布局文件。


tab_item_view.xml   tabbutton的布局文件。button由圖片ImageView和文字TextView兩部分組成

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:orientation="vertical" >

  <ImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:padding="3dp" 
    android:src="@drawable/bottom_bar">
  </ImageView>

  <TextView
    android:id="@+id/textview"	   
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text=""
    android:textSize="12sp"
    android:textColor="#FFFFFF">
  </TextView>

</LinearLayout>


fragment1.xml   頁面fragment的布局文件 C1FFC1是背景顏色。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:background="#C1FFC1" >
</LinearLayout>

fragment2.xml   這里僅僅是顏色不同而已

  android:background="#EEEE00"

fragment3.xml   這里僅僅是顏色不同而已

  android:background="#FFFFFF"

fragment4.xml   這里僅僅是顏色不同而已

  android:background="#C6555D"

fragment5.xml   這里僅僅是顏色不同而已

  android:background="#000000"

Fragment1.java 就讀一下布局文件
package com.fable.helloworld;

import com.fable.helloworld.R;
import com.fable.helloworld.R.layout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class Fragment1 extends Fragment{

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    
    return inflater.inflate(R.layout.fragment1, null);	//fragment2345事實上也就是這里不同而已。	
  }	
}

HelloWorldActivity.java 主要邏輯實現

package com.fable.helloworld;
 
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.fable.helloworld.Fragment1;
import com.fable.helloworld.R;


public class HelloWorldActivity extends FragmentActivity {  //FragmentActivity 能夠對Fragment進行操作的Activity

	  private FragmentTabHost mTabHost;
 
	  //布局填充器 
	  private LayoutInflater mLayoutInflater;

	  //Fragment數組界面 
	  private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,
	      Fragment3.class, Fragment4.class, Fragment5.class }; 

	  //存放圖片數組
		private int mImageArray[] = { R.drawable.home,
		R.drawable.ic_dialog_email, R.drawable.ic_menu_my_calendar,
		R.drawable.ic_search_category_default, R.drawable.ic_input_add };
	 
	  //選項卡文字  
	  private String mTextArray[] = { "首頁", "消息", "好友", "搜索", "很多其它" };
 
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_hello_world);

	    initView();//初始化視圖
	  }

	  /**
	   * 初始化組件
	   */
	  private void initView() {
	    mLayoutInflater = LayoutInflater.from(this);//布局填充,動態布局用到

	    
	    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);// 找到TabHost選項卡
	    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);//選項卡容器
	    // 得到fragment的個數
	    int count = mFragmentArray.length;
	    for (int i = 0; i < count; i++) {
	      // 給每一個Tabbutton設置圖標、文字和內容
	      TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i]).setIndicator(getTabItemView(i));
	      // 將Tabbutton加入進Tab選項卡中
	      mTabHost.addTab(tabSpec, mFragmentArray[i], null);//第二個參數就是選項卡相應頁面的詳細內容
	      // 設置Tabbutton的背景
	      mTabHost.getTabWidget().getChildAt(i)
	          .setBackgroundResource(R.drawable.bottom_bar);
	    }
	  }

	  /**
	   *
	   * 給每一個Tabbutton設置圖標和文字
	   */
	  private View getTabItemView(int index) {
	    View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);//tab的動態布局
	    ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
	    imageView.setImageResource(mImageArray[index]);//設置圖標
	    TextView textView = (TextView) view.findViewById(R.id.textview);
	    textView.setText(mTextArray[index]);//設置文字
	    //這里能夠看出,往一個視圖view里面插東西。就是先通過id找出里面元素對象,然后在對對象進行操作
	    return view;
	  }
}


代碼不多。但文件多了一點,所以上傳了代碼, 點擊打開鏈接





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM