(一)TabHost控件,默认是在顶部显示的
TabHost是盛放Tab按钮和Tab内容的首要容器,
TabWidget(tabs标签)用于选择页面,是指一组包含文本或图标的 ,FrameLayout 用于显示页面的内容,是构成Tab页的容器。
注意: (使用系统自带的id,格式为@android:id/)
TabHost (@android:id/tabhost)
FrameLayout(@android:id/tabcontent),
TabWidget( @android:id/tabs)
(二)TabHost的两种跳转方式
一种是利用Layout:
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(Tab1,getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab1));
一种是利用Intent:
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(Tab1,getResources().getDrawable(R.drawable.icon)).setContent(new Intent(this,OtnerActivity.class)));
Tabhost的activity文件分为两种情况,一种是继承自Activity,一种是继承者TabActivity,二者的不同之处在于tabhost的初始化方式不同,跳转的方式相同。
继承Activity :
setContentView(R.layout.***); //设置上面的xml文件
TabHost tabhost = (TabHost) findViewById(R.id.tabhost);
tabhost.setup(); // 初始化TabHost容器
注意加.setup(),否则会有NullPointer的异常
继承TabActivity 类,
TabHost tabHost = getTabHost();//获取当前TabHost对象
LayoutInflater.from(this).inflate(R.layout.main,tabHost.getTabContentView(), true); //设置使用tabhost布局
添加Tab分页标签(添加选项卡及设置选项的标题及内容 我们知道添加选项卡需要指定一个TabSpec对象,通过TabHost的newTabSpec(选项卡的标识)可以得到,)
tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1", getResources().getDrawable(R.drawable.a1)) .setContent(R.id.view1));
所谓TabSpec就是在Tabwidget中显示的那一个个的小格子,addTab(TabSpec)就是增加一个小格子。
TabSpec主要的方法就是setContent()和setIndicator(),设置的参数不同,设置的内容不同,(详解见上面跳转方式的两个例子)
现象: 在Tabhsot中使用intent去打开一个界面是给 Tabhsot.Tabspec页通过setcontent(intent)方法实现跳转
用intent携带数据只能使用一次
若使用多次, 跳转到得页面都只能拿到第一次设置的数据内容。
原因:在Tabhsot.Tabspec的setcontent方法中将intent给final了。
(三)一个典型的TabHost的例子:
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost" android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<AnalogClock
android:id="@+id/AnalogClock03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" />
<DigitalClock
android:id="@+id/DigitalClock01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:background="#000000" />
</FrameLayout>
</LinearLayout>
</TabHost>
Java代码:
public class TabDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("演示标签分页〉");
//获取TabHost对象
TabHost tabHost=(TabHost) this.findViewById(R.id.tabhost);
tabHost.setup();
//新建一个newTabSpec,设置标签和图标(setIndicator),设置内容(setContent)
TabHost.TabSpec spec=tabHost.newTabSpec("clockTab");
spec.setContent(R.id.AnalogClock03);
spec.setIndicator("模拟时钟", getResources().getDrawable(android.R.drawable.ic_btn_speak_now));
tabHost.addTab(spec);
spec=tabHost.newTabSpec("buttonTab");
spec.setContent(R.id.DigitalClock01);
spec.setIndicator("数字时钟",getResources().getDrawable(android.R.drawable.btn_star_big_on));
tabHost.addTab(spec);
// 设置TabHost的背景颜色
tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
// 设置TabHost的背景图片资源
// tabHost.setBackgroundResource(R.drawable.bg);
// 设置当前现实哪一个标签 tabHost.setCurrentTab(0);
// 0为标签ID
// 标签切换处理,用setOnTabChangedListener
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
Toast.makeText(TabDemoActivity.this, "This is a Test!",
Toast.LENGTH_LONG).show();
}
});
} }
(四)android实现底部TabHost
在TabWidget控件中,通过设置android:layout_alignParentBottom="true"属性实现底部TabHost
(五)TabHost改进
如果不希望默认加载事选中一项,而是做成新浪微博底部控制栏的风格,则需要给TabWidget控件添加属性android:visibility="gone",让后给TabWidget添加若干RadioButton子控件,然后将RadioButton设置成自己想要的样式即可。
参考网址:
http://blog.sina.com.cn/s/blog_8373f9b501018b45.html
http://www.cnblogs.com/over140/archive/2011/03/02/1968042.html
转载于:https://www.cnblogs.com/butterfly-clover/p/3369902.html