使用tab layout时,必须用tabHost和TabWidget,根节点必须是TabHost;包含一个布局,TabWidget(显示tab),FrameLayout(显示内容),点击tab切换不同的内容,有两种不同的方式。
第一种是没一个tab对应一个activity,第二种是,所有的tab都在一个activity中使用。
代码都是从文档上复制而来:下面展示的第一种,第一种更灵活,订制更复杂的信息
main.xml
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <!-- tab 显示 --> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- 内容 --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" /> </LinearLayout> </TabHost>
activity
public class TabWidgetActivity extends TabActivity {
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this, ArtistsActivity.class); //新建一个TabSpec tabHost.newTabSpec(tag名).setIndicator(CharSequence label, 显示的activity xml文件).setContent(设置到intent); spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent); tabHost.addTab(spec);//添加到TabHost // Do the same for the other tabs intent = new Intent().setClass(this, AlbumsActivity.class); spec = tabHost.newTabSpec("albums").setIndicator("Albums", res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SongsActivity.class); spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.ic_tab_songs)).setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTab(2);//当前显示哪个tab } }
在res文件夹下新建一个drawable,下有三个activty的layout布局文件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When selected, use grey 图片--> <item android:drawable="@drawable/ic_tab_artists_grey" android:state_selected="true" /> <!-- When not selected, use white 图片 --> <item android:drawable="@drawable/ic_tab_artists_white" /> </selector>
关于selector标签的用法:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize=["true" | "false"] android:dither=["true" | "false"] android:variablePadding=["true" | "false"] > <item android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_hovered=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_activated=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector>
其他三个activity文件内容都一样,可以给各个activity用不同的布局,比如listView等:
public class AlbumsActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is the AlbumsActivity tab"); setContentView(textview); } }