Android:TabWidget

1.新建类MainTabActivity,继承自TabActivity;FirstTabActivity、SecondTabActivity,继承自Activity

2.修改main.xml文件内容为:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id
="@+id/android: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="@+id/android:tabs"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content" />
<FrameLayout
android:id="@+id/android:tabcontent"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:padding
="5dp" />
</LinearLayout>
</TabHost>

3.在RES文件夹下新建文件夹drawable,然后建立first.xml (android的selector是在drawable/xxx.xml中配置的)

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_tab_artists_grey" android:state_selected="true"/>
<item android:drawable="@drawable/ic_tab_artists_white" />
</selector>

4.在MainTabActivity里编写代码:

public class MainTabActivity extends TabActivity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Resources resources = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;

// 往TabWidget添加内容
intent = new Intent(MainTabActivity.this, FirstTabActivity.class); // 一般数据类型为context的都是指代本类
spec = tabHost.newTabSpec("firstTab")
.setIndicator("First", resources.getDrawable(R.drawable.first))
.setContent(intent);
tabHost.addTab(spec);

}

}

5.android里,如果添加了一个新的activity(或者说是类),必须在AndroidManifest.xml里的application下添加activity

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainTabActivity"
android:label
="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".FirstTabActivity" />
<activity android:name=".SecondTabActivity" />
</application>

6.tabwidget所需要的图片我放在drawable-mdpi里

源码:http://files.cnblogs.com/kelei12399/MyTabWidget.zip

你可能感兴趣的:(tabwidget)