双层TabHost嵌套

主TabHost

public class MainActivity extends ActivityGroup{


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup(this.getLocalActivityManager());

		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("First Tab")
				.setContent(new Intent(this, MainActivity1.class)));

		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Second Tab")
				.setContent(new Intent(this, MainActivity1.class)));

		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Third Tab")
				.setContent(new Intent(this, MainActivity1.class)));
		tabHost.setCurrentTab(0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

主TabHost 布局文件

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
              <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                >
            </TabWidget>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </FrameLayout>
          
        </RelativeLayout>
    </TabHost>
 
</RelativeLayout>

  子TabHost

public class MainActivity1 extends Activity {

	private ListView listView;
	private List<String> currentData;// 当前视图显示的数据
	private CustomAdapter customadapter;// 自定义适配器
	private View loadingView;// 加载视图的布局

	private int currentPage = 1;// 当前页,默认为1
	private int pageSize = 20;// 每页显示十条信息
	private int last_item_position;// 最后item的位置

	private boolean isLoading = false;// 是否加载过,控制加载次数

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main1);

		TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup();
		TabWidget tabWidget = tabHost.getTabWidget();
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("First Tab")
				.setContent(R.id.tab1));

		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Second Tab")
				.setContent(R.id.tab2));

		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Third Tab")
				.setContent(R.id.tab3));
		tabHost.setCurrentTab(0);
		int height = 30;
		// int width =45;
		for (int i = 0; i < tabHost.getChildCount(); i++) {

			/** 设置高度、宽度,由于宽度设置为fill_parent,在此对它没效果 */
			tabWidget.getChildAt(i).getLayoutParams().height = height;
			// tabWidget.getChildAt(i).getLayoutParams().width = width;
		}
		tabHost.setOnTabChangedListener(new MyTabListener(this));
	}

布局文件

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
 
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
 
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
 
             <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
 				<ListView android:id="@+id/lv_id"
				 android:layout_width="fill_parent"
				 android:layout_height="wrap_content" />         
                </LinearLayout>
 
                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    <Button
                        android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Second Tab Content" />
                </LinearLayout>
 
                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    <Button
                        android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Third Tab Content" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
 
</RelativeLayout>


你可能感兴趣的:(双层TabHost嵌套)