在该项目中首先是做一个TabHost界面,本来对Android知识都不是很了解,现在只有查询SDK来开始学习Tab选项。一步一步的学习……
1、TabHost用法
HelloTabWidget.java:
对应的布局文件:main.xmlpublic class HelloTabWidget extends TabActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost.取得TablHost引用 // Resusable TabSpec for each tab.对于每一个tab,TabHost.TabSpec被用来定义tab属性 TabHost.TabSpec spec; 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); /* * * Initialize a TabSpec for each tab and add it to the TabHost * newTabSpec(String) 创建一个新的TabHost.TabSpec实例 * setIndicator(CharSequence, Drawable) 设置标签的文本和图标 * */ spec = tabHost.newTabSpec("artists") .setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(intent);//打开Activity 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_artists)) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SongsActivity.class); spec = tabHost .newTabSpec("songs") .setIndicator("Songs", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(intent); tabHost.addTab(spec); //在最后,打开一个Tab显示为默认:setCurrentTab(int),int是要打开的Tab的索引值 tabHost.setCurrentTab(2); /* * 需要注意: * Notice that not once was the TabWidget object referenced. * This is because a TabWidget must always be a child of a TabHost, * which is what you use for almost all interaction with the tabs. * So when a tab is added to the TabHost, * it's automatically added to the child TabWidget. */ } }
res/drawable/ic_tab_artists.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"> <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>
AlbumsActivity.java、ArtistsActivity.java、SongsActivity.java代码与下面的内容类似,只不过是改下TextView的内容<?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>
在这个二期项目中上述有一点不明白的是:res/drawable/ic_tab_artists.xml:这个文件的运行机理是什么?public class SongsActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("SongsActivity tab"); setContentView(textview); } }