Android TabHost总结
2015-10-15 16:32
417 查看
听说现在流行用fragment Tabhost 代替TabHost,刚入门,还是从tabHost开始。
TabHost即微信底部的选项卡,直接上代码:
布局文件:
TabHost底下为FrameLayout,FrameLayout底下为LinearLayout,LinearLayout底下的每个LinearLayout就是每个选项卡的页面。
MainActivity中初始化TabHost:
效果如下图所示
![](http://img.blog.csdn.net/20151015163024079?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
【设置标签切换触发】
TabHost即微信底部的选项卡,直接上代码:
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TabHost android:id="@+id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="texttab1" /> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/tab4" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> </LinearLayout> </TabHost> </LinearLayout>
TabHost底下为FrameLayout,FrameLayout底下为LinearLayout,LinearLayout底下的每个LinearLayout就是每个选项卡的页面。
MainActivity中初始化TabHost:
public class MainActivity extends Activity { private TabHost tabhost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); tabhost = (TabHost) findViewById(R.id.tabhost); tabhost.setup(); tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("微信") .setContent(R.id.tab1)); tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("通讯录") .setContent(R.id.tab2)); tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("发现") .setContent(R.id.tab3)); tabhost.addTab(tabhost.newTabSpec("tab4").setIndicator("我") .setContent(R.id.tab4)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
效果如下图所示
【设置标签切换触发】
tabHost.setOnTabChangedListener(new OnTabChangeListener(){ @Override public void onTabChanged(String tabId) { if (tabId.equals("tab1")) { //第一个标签 } if (tabId.equals("tab2")) { //第二个标签 } if (tabId.equals("tab3")) { //第三个标签 } } });
相关文章推荐
- Android listview与adapter用法
- 正规的Handler写法
- android 字符串截取
- Android_03_单元测试框架
- View、ViewGroup的点击事件处理过程
- Android之Handler用法总结
- Android中轻松使用线程
- Android 中Activity,Window和View之间的关系
- Android adb连接异常情况处理
- android onTouch事件详解
- Android Studio下的NDK-JNI开发总结
- 程序进入Android Market(网页或者应用)
- Android开发模板------SlidingPaneLayout、Navigation Drawer的基本介绍
- android bitMap总结
- 获取android签名相关信息
- SlidingMenu条目点击无响应、Android输入框过多视图改变和ViewPager页面点击切换自然
- Android推送通知消息管理
- Android 23 Connot resolve method updateNotification.setLatestEventInfo()解决方案
- Android:根据上下文Context获取Activity
- AndroidSQLite建表语句