android极简原创系列:tabhost最简单配置

第一步配置布局文件


<TabHost
        android1:id="@android:id/tabhost"
        android1:layout_width="match_parent"
        android1:layout_height="match_parent"
        android1:layout_weight="1" >

        <LinearLayout
            android1:layout_width="match_parent"
            android1:layout_height="match_parent"
            android1:orientation="vertical" >

            <TabWidget
                android1:id="@android:id/tabs"
                android1:layout_width="match_parent"
                android1:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android1:id="@android:id/tabcontent"
                android1:layout_width="match_parent"
                android1:layout_height="match_parent" >

                <LinearLayout
                    android1:id="@+id/tab1"
                    android1:layout_width="match_parent"
                    android1:layout_height="match_parent" >

                    <ListView
                        android1:id="@+id/listview1"
                        android1:layout_width="fill_parent"
                        android1:layout_height="fill_parent" >
                    </ListView>
                </LinearLayout>

                <LinearLayout
                    android1:id="@+id/tab2"
                    android1:layout_width="match_parent"
                    android1:layout_height="match_parent" >
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
上面这些大部分照抄即可,关键点在
<LinearLayout
                    android1:id="@+id/tab2"
                    android1:layout_width="match_parent"
                    android1:layout_height="match_parent" >
                </LinearLayout>


在linearlayout中设置你需要的控件,我这里放了一个listview

第二步 配置activity

final TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);
		tabhost.setup();
		TabWidget tabwidget = tabhost.getTabWidget();
		tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("tab1")
				.setContent(R.id.tab1));
		tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("tab2")
				.setContent(R.id.tab2));
我这里新建了两个tab,如果要继续增加根据上面的规律添加即可!

第三步 事件监听

tabhost.setOnTabChangedListener(new OnTabChangeListener() {
		 public void onTabChanged(String arg0) {
		 // TODO Auto-generated method stub
		 Toast.makeText(DiquActivity.this, "当前标签页为" + arg0,
		 Toast.LENGTH_SHORT).show();
		 }
		 });

		tabhost.getTabWidget().getChildAt(0)
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						tabhost.setCurrentTab(0);
						Toast.makeText(DiquActivity.this, "当前标签页为1",
								Toast.LENGTH_SHORT).show();
					}
				});

		tabhost.getTabWidget().getChildAt(1)
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						tabhost.setCurrentTab(1);
						Toast.makeText(DiquActivity.this, "当前标签页为2",
								Toast.LENGTH_SHORT).show();
					}
				});
很容易懂吧,我就不解释了。照着做即可。android4.0环境下通过。




你可能感兴趣的:(android,tabhost,简单)