android极简原创系列:tabhost最简单配置
16lz
2021-01-25
第一步配置布局文件
<TabHost android1:id="@android:id/tabhost" android1:layout_width="match_parent" android1:layout_height="match_parent" android1:layout_weight="1" > <LinearLayout android1:layout_width="match_parent" android1:layout_height="match_parent" android1:orientation="vertical" > <TabWidget android1:id="@android:id/tabs" android1:layout_width="match_parent" android1:layout_height="wrap_content" > </TabWidget> <FrameLayout android1:id="@android:id/tabcontent" android1:layout_width="match_parent" android1:layout_height="match_parent" > <LinearLayout android1:id="@+id/tab1" android1:layout_width="match_parent" android1:layout_height="match_parent" > <ListView android1:id="@+id/listview1" android1:layout_width="fill_parent" android1:layout_height="fill_parent" > </ListView> </LinearLayout> <LinearLayout android1:id="@+id/tab2" android1:layout_width="match_parent" android1:layout_height="match_parent" > </LinearLayout> </FrameLayout> </LinearLayout> </TabHost>上面这些大部分照抄即可,关键点在
<LinearLayout android1:id="@+id/tab2" android1:layout_width="match_parent" android1:layout_height="match_parent" > </LinearLayout>
在linearlayout中设置你需要的控件,我这里放了一个listview
第二步 配置activity
final TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);tabhost.setup();TabWidget tabwidget = tabhost.getTabWidget();tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.tab1));tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.tab2));我这里新建了两个tab,如果要继续增加根据上面的规律添加即可!
第三步 事件监听
tabhost.setOnTabChangedListener(new OnTabChangeListener() { public void onTabChanged(String arg0) { // TODO Auto-generated method stub Toast.makeText(DiquActivity.this, "当前标签页为" + arg0, Toast.LENGTH_SHORT).show(); } });tabhost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtabhost.setCurrentTab(0);Toast.makeText(DiquActivity.this, "当前标签页为1",Toast.LENGTH_SHORT).show();}});tabhost.getTabWidget().getChildAt(1).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtabhost.setCurrentTab(1);Toast.makeText(DiquActivity.this, "当前标签页为2",Toast.LENGTH_SHORT).show();}});很容易懂吧,我就不解释了。照着做即可。android4.0环境下通过。
更多相关文章
- 安全新手入坑——HTML标签
- Android(安卓)百分比布局详解及实例代码
- Android动画四种类型xml布局
- 采用Scroller实现任意布局的上下左右弹性效果
- CheckBox控件中background和button的区别
- Android(安卓)ZoomControls缩放控件
- lint工具改错
- Android控件之ImageButton
- android Caused by: java.lang.IllegalArgumentException: Binar