展会信息港展会大全

Android自定义tabhost实现
来源:互联网   发布日期:2015-10-03 11:11:33   浏览:2101次  

导读:tabhost在网上随便找一下很多,但是真正全的东西还是不多,这里总结下我的tabhost的实现的全过程。1、定义tabhost的xml文件TabHost xmlns:android=http://schemas.android.com/apk/res/android andro......

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;

@Override

protected 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();

}

@Override

protected 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完成

赞助本站

人工智能实验室

相关热词: 自定义tabhost

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港