TabHost 控件有整个工程,0分下载

来源:互联网 发布:国际歌在中国禁止知乎 编辑:程序博客网 时间:2024/06/09 21:15
要使用TabHost,有两种方式:使用TabHost控件和继承TabActivity。
1.使用TabHost控件
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hometabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000">
<!-- tabcontent和TabWidget需用一个Layout包含起来-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- FrameLayout的id属性必须为 @android:id/tabcontent -->
<!-- 这个区域存放在TabHost中展示的view,也可以用Intent来包含外部Activity -->
<!-- 注意被包含的Activity的生命周期,如果新跳转tab包含内容为View,则被隐藏Activity执行onPause() -->
<!-- 如果新跳转tab包含内容为Activity,则被隐藏Activity执行onPause()->onStop() -->
<!-- 只有父Activity被销毁,被包含Activity才会销毁.需注意包含多个Activity时,被包含Activity的销毁顺序,没测试过 -->
<!-- tabcontent中view和被包含Activity数目和为选项卡数量 -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="1111"/>
<TextView
android:id="@+id/view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="2222"/>
<TextView
android:id="@+id/view3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="3333"/>
</FrameLayout>
<!-- 由于TabWidget比较难看,且无法修改,可以使用一组按钮(一般用RadioButton)配合setCurrentTab()和startActivity()控制tab的切换 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click 1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click 2"/>
</LinearLayout>
<!-- TabWidget的id属性必须为 @android:id/tabs -->
<!-- TabWidget与tabcontent的相对位置就是界面中2者的位置 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="horizontal">
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
// 注意,如果被包含对象含有Activity,那么必须继承ActivityGroup而不是Activity(TabActivity继承ActivityGroup)
publicclass TabHosttActivity extends ActivityGroup implements OnClickListener {
TabHost tabHost;
Button btn1, btn2;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = (TabHost) findViewById(R.id.tabhost);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
//注意,如果被包含对象含有Activity,tabHost.setup(getLocalActivityManager())不是tabHost.setup()
tabHost.setup(getLocalActivityManager());
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("标签1")
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("标签2")
.setContent(R.id.view2));
// .setContent(new Intent(this, TabSpecContent.class)));
tabHost.addTab(tabHost
.newTabSpec("tab3")
.setIndicator("标签3",
getResources().getDrawable(R.drawable.ic_launcher))
// .setContent(R.id.view3));
//如果tab包含对象为Activity,就不要为改tab写view了
//如果tab包含对象为Activity,注意该Activity在manifest.xml中配置
.setContent(new Intent(this, TabSpecContent.class)));
tabHost.setCurrentTabByTag("tab2");
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
publicvoid onTabChanged(String tabId) {
//TODO Auto-generated method stub
}
});
// TabWidget tabWidget = tabHost.getTabWidget();
// tabWidget.setBackgroundColor(Color.GREEN);
// tabWidget.
}
@Override
publicvoid onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
tabHost.setCurrentTab(0);
break;
case R.id.btn2:
tabHost.setCurrentTab(2);
break;
default:
break;
}
}
} 2.继承TabActivity
<?xmlversion="1.0"encoding="utf-8"?>
<TabHostxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 必须设置 id="@android:id/tabhost" -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- TabWidget和tabcontent也不可少 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tabactivity11"/>
<TextView
android:id="@+id/view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tabactivity2222"/>
<TextView
android:id="@+id/view3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tabactivity3333"/>
</FrameLayout>
</LinearLayout>
</TabHost>
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("标签1")
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("标签2")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("标签3")
.setContent(
//如果tab包含对象为Activity,就不要为改tab写view了
//如果tab包含对象为Activity,注意该Activity在manifest.xml中配置
new Intent(MyTabActivity.this, TabSpecContent.class)));
// .setContent(R.id.view3));
}
原创粉丝点击