Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> android FragmentTabhost實現選項卡

android FragmentTabhost實現選項卡

編輯:Android開發教程

在Android3.0之後,google創造了Fragment,因此原來的Tabhost已經不推薦使用了,現在一般推薦使用FragmentTabhost。

google考慮到了兼容問題,因此FragmentTabhost並未加在官方的SDK中,而是把它放在了android-support-v4.jar中

下面我帶大家來實現一下這個功能。

1、MyFragmentTabhostActivity.java

public class MyFragmentTabhostActivity extends FragmentActivity {

	private Context context;
	private FragmentTabHost fragmentTabHost = null;
	private Class[] fragmentArray = { MyFragment.class, MyFragment.class, MyFragment.class, MyFragment.class };
	private int[] tabImageArray = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
	private String[] tabTextArray = { "廣場", "排名", "商城", "我" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragmenttabhost);
		context = this;
		initView();
	}

	private void initView() {
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		fragmentTabHost.setup(context, getSupportFragmentManager(), R.id.real_tabcontent);
		for (int i = 0; i < fragmentArray.length; i++) {
			// 給每個Tab按鈕設置圖標、文字和內容
			TabSpec tabSpec = fragmentTabHost.newTabSpec(tabTextArray[i]).setIndicator(getTabItemView(inflater, i));
			fragmentTabHost.addTab(tabSpec, fragmentArray[i], null);
			// 設置Tab按鈕的背景
			fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.rgb((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
		}
	}

	public View getTabItemView(LayoutInflater inflater, int index) {
		View view = inflater.inflate(R.layout.item_tab, null);
		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		TextView textView = (TextView) view.findViewById(R.id.textview);
		imageView.setImageResource(tabImageArray[index]);
		textView.setText(tabTextArray[index]);
		return view;
	}
}

URL:http://www.bianceng.cn/OS/extra/201609/50439.htm

2、Fragment.java

public class MyFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_fragment, null);
		view.setBackgroundColor(Color.rgb((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
		return view;
	}
}

3、activity_fragment.xml

<?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" >

    <FrameLayout
        android:id="@+id/real_tabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"/>
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

4、item_tab.xml

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
    </LinearLayout>

</RelativeLayout>

5、layout_fragment.xml

<?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" >

</LinearLayout>

6、運行截圖

    No
  1. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved