sgafdsg 2011-03-26
AndroidTabWidget/TabHost有两种使用方法:
第一种:使用系统自带写好的TabHost(及继承自TabActivity类)
第二种:就是定义我们自己的tabHost:不用继承TabActivity
这里我们使用第二章自定义tabHost的方法实现
示例:
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="5dip">
<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="5dip">
</FrameLayout>
</LinearLayout>
</TabHost>Activity.java
public class MainActivity extends TabActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
TabHost tabHost=this.getTabHost();
Intent remoteIntent=new Intent();
remoteIntent.setClass(this, mp3player.class);
//内部类的写法,TabSpec代表一页的内容
TabHost.TabSpec remoteSpec=tabHost.newTabSpec("remote");
//使用android内部的图标,也可以使用自行下载的图片 代表了一页
Resources res=this.getResources();
remoteSpec.setIndicator("remote", res.getDrawable(android.R.drawable.arrow_down_float));
remoteSpec.setContent(remoteIntent);
tabHost.addTab(remoteSpec);
Intent localIntent=new Intent();
localIntent.setClass(this, LocalActivity.class);
TabHost.TabSpec localSpec=tabHost.newTabSpec("local");
localSpec.setIndicator("local", res.getDrawable(android.R.drawable.arrow_down_float));
localSpec.setContent(localIntent);
tabHost.addTab(localSpec);
}
}