FragmentTabHost and Fragment 用法

主要布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


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


        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1" />
    </android.support.v4.app.FragmentTabHost>



    <!--决定tab在上还是在底部-->
    <FrameLayout
        android:id="@+id/tabcontainer"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />


</LinearLayout>


主要代码如下:

package com.example.testtab;

import android.app.Activity;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

	private FragmentTabHost tabHost;
	private TabWidget tabWidget;
	private TabSpec tabSpec;
	private View view;
	private View view2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
		tabHost.setup(this, getSupportFragmentManager(),R.id.tabcontainer);
		tabHost.getTabWidget().setDividerDrawable(null);
		view = getLayoutInflater().inflate(R.layout.view,null);
		tabHost.addTab(tabHost.newTabSpec("home").setIndicator(view),HomeFragment.class,null);
		view2 = getLayoutInflater().inflate(R.layout.view2,null);
		tabHost.addTab(tabHost.newTabSpec("find").setIndicator(view2),FindFragment.class,null);
	}
}




你可能感兴趣的:(Fragment,FragmentTabHost)