自定义tabhost实现
16lz
2021-12-04
tabhost在网上随便找一下很多,但是真正全的东西还是不多,这里总结下我的tabhost的实现的全过程。
1、定义tabhost的xml文件
arent" android:layout_height="fill_parent" >
特别注意要加几个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
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完成
转载于:https://my.oschina.net/alishow527/blog/94471
更多相关文章
- TabHost与RadioGroup结合完成的菜单
- 安装Intel HAXM为Android(安卓)模拟器加速,30秒内启动完成
- 高级组件之自动完成文本框
- Android(安卓)API 指南
- 【自学Android】使用DataBinding,ViewModel,LiveData完成点赞小
- Android:自动完成文本框
- 2014年 Android(安卓)学习计划
- Android(安卓)Studio 初体验
- 2.5.6 使用progressDialog创建进度对话框