tabhost在网上随便找一下很多,但是真正全的东西还是不多,这里总结下我的tabhost的实现的全过程。

1、定义tabhost的xml文件

<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:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical" >                 <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="fill_parent"            android:layout_height="0.0dip"            android:layout_weight="1.0" />                   <TabWidget            android:id="@android:id/tabs"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="0.0"            android:visibility="gone"            android:divider="@drawable/businessopp_line" />                    <RadioGroup            android:id="@+id/tabhost_radio"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_gravity="bottom"            android:gravity="center_vertical"            android:orientation="horizontal" >            <RadioButton                android:id="@+id/radio_button1"                style="@style/tab_style"                android:background="@drawable/style_tabhost_btn1"                android:tag="radio_button0" />            <RadioButton                android:id="@+id/radio_button2"                style="@style/tab_style"                android:background="@drawable/style_tabhost_btn2"                android:tag="radio_button1"/>            <RadioButton                android:id="@+id/radio_button3"                style="@style/tab_style"                android:background="@drawable/style_tabhost_btn3"                android:tag="radio_button2" />            <RadioButton                android:id="@+id/radio_button4"                style="@style/tab_style"                android:background="@drawable/style_tabhost_btn4"                android:tag="radio_button3" />            <RadioButton                android:id="@+id/radio_button5"                style="@style/tab_style"                android:background="@drawable/style_tabhost_btn5"                android:tag="radio_button4" />                    </RadioGroup>            </LinearLayout></TabHost>

特别注意要加几个id: tabhost中的android:id="@android :id/tabhost" FramFrameLayout中的android:id="@android :id/tabcontent" TabWidget中的android:id="@android :id/tabs"一定要添加,否则报错。还有个地方值得注意的,就是FramFrameLayout中的android:layout_weight="1.0"和TabWidget中的android:layout_weight="0.0"用来控制tabhost组件显示在屏幕最下方。

其中style/tab_style

<style name="tab_style">        <item name="android:button">@null</item>          <item name="android:ellipsize">marquee</item>        <item name="android:gravity">center_horizontal</item>          <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:layout_weight">1.0</item>    </style>

2、写一个继承TabActivity的类,要写在onstart方法里

public class TabHostActivity extends TabActivity {private TabHost tabHost;public static final String TAB_ITEM_1 = "tabItem1";public static final String TAB_ITEM_2 = "tabItem2";public static final String TAB_ITEM_3 = "tabItem3";public static final String TAB_ITEM_4 = "tabItem4";public static final String TAB_ITEM_5 = "tabItem5";private RadioGroup radiogroup;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_tabhost);WelcomeActivity.myActivitiesList.add(this);radiogroup = (RadioGroup) findViewById(R.id.tabhost_radio);radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.radio_button1:tabHost.setCurrentTabByTag(TAB_ITEM_1);break;case R.id.radio_button2:tabHost.setCurrentTabByTag(TAB_ITEM_2);break;case R.id.radio_button3:tabHost.setCurrentTabByTag(TAB_ITEM_3);break;case R.id.radio_button4:tabHost.setCurrentTabByTag(TAB_ITEM_4);break;case R.id.radio_button5:tabHost.setCurrentTabByTag(TAB_ITEM_5);break;default:break;}}});tabHost = getTabHost();}@Overrideprotected void onStart() {super.onStart();TabSpec tab1b = tabHost.newTabSpec(TAB_ITEM_1);TabSpec tab2b = tabHost.newTabSpec(TAB_ITEM_2);TabSpec tab3b = tabHost.newTabSpec(TAB_ITEM_3);TabSpec tab4b = tabHost.newTabSpec(TAB_ITEM_4);TabSpec tab5b = tabHost.newTabSpec(TAB_ITEM_5);tab1b.setIndicator(TAB_ITEM_1).setContent(new Intent(this, Test.class));tab2b.setIndicator(TAB_ITEM_2).setContent(new Intent(this, Test.class));tab3b.setIndicator(TAB_ITEM_3).setContent(new Intent(this, Test.class));tab4b.setIndicator(TAB_ITEM_4).setContent(new Intent(this, Test.class));tab5b.setIndicator(TAB_ITEM_5).setContent(new Intent(this, Test.class));tabHost.addTab(tab1b);tabHost.addTab(tab2b);tabHost.addTab(tab3b);tabHost.addTab(tab4b);tabHost.addTab(tab5b);}}

完成,一个自定义的tabhost完成

更多相关文章

  1. Android(安卓)Studio的安装,史上最详细(超多图)!!
  2. android studio mac_最新Mac安装Flutter全过程
  3. Android工具包AndroidUtils
  4. Android自动完成文本框
  5. Android自动完成文本框
  6. Android自动完成文本框
  7. android SQLite
  8. android学习3 进度条
  9. Android(安卓)Provision (Setup Wizard)

随机推荐

  1. Android之service探究
  2. Android(安卓)LinearLayout及TextView的
  3. android edittext 限制输入框小数位数
  4. Android(安卓)- ContactPicker(选择联系
  5. android 计时器线程
  6. android greenDao3.2.2存储数据
  7. android:动态创建多个按钮 并给每个按键添
  8. android 预置apk so库 jar包等
  9. Android(安卓)Studio之Instant Run
  10. Android(安卓)- 信息列表页面【仿】淘宝A