package com.example.tabhost_1; import android.app.ActivityGroup; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TabHost; import android.widget.TabHost.TabSpec; import android.widget.TextView; public class MainActivity extends ActivityGroup { private TabHost tabHost;// 声明一个TabHost对象 private RadioGroup radioGroup;// 声明一个RadioGroup对象 // 资源文件 private Class activitys[] = { TabActivity1.class, TabActivity2.class, TabActivity3.class, TabActivity4.class, TabActivity5.class };// 跳转的Activity private String title[] = { "首页", "搜索", "设置", "主题", "更多" };// 设置菜单的标题 private int image[] = { R.drawable.first, R.drawable.search, R.drawable.set, R.drawable.theme, R.drawable.more, };// 设置菜单 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initTabView();// 初始化tab标签 } private void initTabView() { // 实例化tabhost this.tabHost = (TabHost) findViewById(R.id.mytabhost); // 由于继承了ActivityGroup,所以需要在setup方法里加入此参数,若继承TabActivity则可省略 tabHost.setup(this.getLocalActivityManager()); // 创建标签 for (int i = 0; i < activitys.length; i++) { /* * 由于采用了RadioGroup,样式已经在xml里设置,故这里无需再自定义view //实例化一个view作为tab标签的布局 * View view=View.inflate(this, R.layout.tab_layout, null); * * //设置imageview ImageView imageView=(ImageView) * view.findViewById(R.id.image); * imageView.setImageDrawable(getResources().getDrawable(image[i])); * //设置textview TextView textView=(TextView) * view.findViewById(R.id.title); textView.setText(title[i]); */ // 设置跳转activity Intent intent = new Intent(this, activitys[i]); // 载入view对象并设置跳转的activity TabSpec spec = tabHost.newTabSpec(title[i]).setIndicator(title[i]) .setContent(intent); // 添加到选项卡 tabHost.addTab(spec); } radioGroup = (RadioGroup) findViewById(R.id.radiogroup); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radio1: // tabHost.setCurrentTab(int id); 用当前所在页数来跳转activity // tabHost.setCurrentTabByTag(String tag); 用当前标签来跳转activity tabHost.setCurrentTabByTag(title[0]); break; case R.id.radio2: tabHost.setCurrentTabByTag(title[1]); break; case R.id.radio3: tabHost.setCurrentTabByTag(title[2]); break; case R.id.radio4: tabHost.setCurrentTabByTag(title[3]); break; case R.id.radio5: tabHost.setCurrentTabByTag(title[4]); break; } } }); } }
package com.example.tabhost_1; import android.app.Activity; import android.os.Bundle; public class TabActivity1 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabactivity1); } }
--------------------------------------------------------------------------------------------
activity_main.xml:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mytabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 需要一个布局管理器 --> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 由于TabHost是继承于FrameLayout,所以需要一个FrameLaytout布局(内容页) ,id 必须为tabcontent --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" > </FrameLayout> <!-- TabWidget必须标签,用来存放tab标签,且id必须为tabs --> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:visibility="gone" > </TabWidget> <RadioGroup android:id="@+id/radiogroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:padding="3dp" > <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/first" android:gravity="center_horizontal" android:text="首页" android:textSize="10sp" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/search" android:gravity="center_horizontal" android:text="搜索" android:textSize="10sp" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/set" android:gravity="center_horizontal" android:text="设置" android:textSize="10sp" /> <RadioButton android:id="@+id/radio4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/theme" android:gravity="center_horizontal" android:text="主题" android:textSize="10sp" /> <RadioButton android:id="@+id/radio5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/more" android:gravity="center_horizontal" android:text="更多" android:textSize="10sp" /> </RadioGroup> </RelativeLayout> </TabHost>----------------------------------------------------------------------------------
tabactivity1.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是界面1"/> </LinearLayout> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.tabhost_1" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" 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="TabActivity1" android:label="@string/app_name" > </activity> <activity android:name="TabActivity2" android:label="@string/app_name" > </activity> <activity android:name="TabActivity3" android:label="@string/app_name" > </activity> <activity android:name="TabActivity4" android:label="@string/app_name" > </activity> <activity android:name="TabActivity5" android:label="@string/app_name" > </activity> </application> </manifest>
TabHost的布局的文件必须遵循下面几点:
1、所有的用于标签配置的文件,必须以“<TabHost>”为根节点;
2、为了保证标签页和标签内容显示正常(例如:标签提示要放在标签显示内容之上)则可以采用一个布局管理器进行布局(例如:LinearLayout,RelativeLayout..)
3、定义一个“<TagWidget>”的标签,用于表示整个标签容器,另外在定义此组件ID为“tabs”,表示允许加入多个标签
-----------------------------------------
这里有个重载方法setIndicator(),这里是用来设置标签页:
1、public TabHost.TabSpec setIndicator(CharSequence label)
设置标题,此时无图标
2、public TabHost.TabSpec setIndicator(CharSequence label, Drawable icon)
设置标题、图标(这里的图标可以用getResources().getDrawable(int id))来设置
3、public TabHost.TabSpec setIndicator(View view)
设置自定义view
还有个setContent(Intent intent),这里是用来设置标签内容的,也就是我们要跳转的Activity
亲测可用