标签:android style blog http color java 使用 os
TabSpec与TabHost
TabHost相当于浏览器中浏览器分布的集合,而Tabspec则相当于浏览器中的每一个分页面。d在Android中,每一个TabSpec分布可以是一个组件,也可以是一个布局,然后将每一个分页装入TabHost中,TabHost即可将其中的每一个分页一并显示出来
Android 中的Tabhost控件是个挺好用的控件,像一些分模块展示的页面就可以用Tabhost。
Tabhost的主要是由TabSpac组成的选项卡集合。TabSpec主要有两个重要方法,看代码:
/** * A tab has a tab indicator, content, and a tag that is used to keep * track of it. This builder helps choose among these options. * * For the tab indicator, your choices are: * 1) set a label * 2) set a label and an icon * * For the tab content, your choices are: * 1) the id of a {@link View} * 2) a {@link TabContentFactory} that creates the {@link View} content. * 3) an {@link Intent} that launches an {@link android.app.Activity}. */ public class TabSpec { private String mTag; private IndicatorStrategy mIndicatorStrategy; private ContentStrategy mContentStrategy; private TabSpec(String tag) { mTag = tag; } /** * Specify a label as the tab indicator. */ public TabSpec setIndicator(CharSequence label) { mIndicatorStrategy = new LabelIndicatorStrategy(label); return this; } /** * Specify a label and icon as the tab indicator. */ public TabSpec setIndicator(CharSequence label, Drawable icon) { mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon); return this; } /** * Specify a view as the tab indicator. */ public TabSpec setIndicator(View view) { mIndicatorStrategy = new ViewIndicatorStrategy(view); return this; } /** * Specify the id of the view that should be used as the content * of the tab. */ public TabSpec setContent(int viewId) { mContentStrategy = new ViewIdContentStrategy(viewId); return this; } /** * Specify a {@link android.widget.TabHost.TabContentFactory} to use to * create the content of the tab. */ public TabSpec setContent(TabContentFactory contentFactory) { mContentStrategy = new FactoryContentStrategy(mTag, contentFactory); return this; } /** * Specify an intent to use to launch an activity as the tab content. */ 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:
Demo下载地址 :http://download.csdn.net/detail/china1988s/4072957
。标签:android style blog http color java 使用 os
原文地址:http://blog.csdn.net/songjunyan/article/details/38461917