Android 中的Tabhost控件是个挺好用的控件,像一些分模块展示的页面就可以用Tabhost。
Tabhost的主要是由TabSpac组成的选项卡集合。TabSpec主要有两个重要方法,看代码:
- public class TabSpec {
-
- private String mTag;
-
- private IndicatorStrategy mIndicatorStrategy;
- private ContentStrategy mContentStrategy;
-
- private TabSpec(String tag) {
- mTag = tag;
- }
-
-
- public TabSpec setIndicator(CharSequence label) {
- mIndicatorStrategy = new LabelIndicatorStrategy(label);
- return this;
- }
-
-
- public TabSpec setIndicator(CharSequence label, Drawable icon) {
- mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon);
- return this;
- }
-
-
- public TabSpec setIndicator(View view) {
- mIndicatorStrategy = new ViewIndicatorStrategy(view);
- return this;
- }
-
-
- public TabSpec setContent(int viewId) {
- mContentStrategy = new ViewIdContentStrategy(viewId);
- return this;
- }
-
-
- public TabSpec setContent(TabContentFactory contentFactory) {
- mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);
- return this;
- }
-
-
- public TabSpec setContent(Intent intent) {
- mContentStrategy = new IntentContentStrategy(mTag, intent);
- return this;
- }
setIndicator()可以设置选项卡得图标和文字。
需要注意几点是: 1、如果你的Tabhost是从xml文件中findViewById()得到的,
TabWidget 必须为 android:id="@android:id/tabs" ,
FrameLayout android:id="@android:id/tabcontent" ;
- <TabHost android:id="@+id/tabhost_info" android:layout_width="fill_parent"
- android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
-
- <LinearLayout android:id="@+id/linearLayout"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:orientation="vertical">
- <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="wrap_content"
- android:layout_gravity="fill">
- <include android:id="@+id/info_include01" layout="@layout/info_layout01" />
- <include android:id="@+id/info_include02" layout="@layout/info_layout02" />
- <include android:id="@+id/info_include03" layout="@layout/info_layout03" />
-
- </FrameLayout>
- </LinearLayout>
-
- </TabHost>
2、代码中,在添加TabWidget前,需要调用setup()方法。
- tabHost=(TabHost)findViewById(R.id.tabhost_info);
- tabHost.setup();
- tabHost.addTab(tabHost.newTabSpec("信息")
- .setContent(R.id.info_include01)
- .setIndicator("基本信息",getResources().getDrawable(R.drawable.ic_launcher))
-
- );
- tabHost.addTab(tabHost.newTabSpec("更多信息")
- .setContent(R.id.info_include02)
- .setIndicator("更多信息",getResources().getDrawable(R.drawable.ic_launcher))
-
-
- tabHost.addTab(tabHost.newTabSpec("附件下载")
- .setContent(R.id.info_include03)
- .setIndicator("附件下载",getResources().getDrawable(R.drawable.ic_launcher))
-
下面是自己写的一个demo:
TabHost简单使用案例
原文:http://www.cnblogs.com/denghaicheng/p/4509082.html