android4.0 FragmentTabHost tabs在底部 仿qq界面
2016-07-12 17:38
330 查看
首先吐槽下官网,http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html ,这里居然没给出xml的配置,导致一直弄了很久。而百度搜索到的大部分都是这个配置,不只是否是版本升级的原因,我按照这个配置,运行就会报错:java.lang.RuntimeException: Your TabHost must have a FrameLayout whose id attribute is ‘android.R.id.tabcontent’
各种尝试,最后发现只有下面这个代码能运行,但是tabs是在顶部的
Xml代码 复制代码 收藏代码
最后只好谷歌了,发现了国外的大神的一个配置,就是FragmentTabHost里面什么都没放置。下面是我自己写的配置,
Xml代码 复制代码 收藏代码
下面是MainActivity.java的代码
Java代码 复制代码 收藏代码
各种尝试,最后发现只有下面这个代码能运行,但是tabs是在顶部的
Xml代码 复制代码 收藏代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
最后只好谷歌了,发现了国外的大神的一个配置,就是FragmentTabHost里面什么都没放置。下面是我自己写的配置,
Xml代码 复制代码 收藏代码
1.<?xml version="1.0" encoding="utf-8"?> 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3. android:layout_width="match_parent" 4. android:layout_height="match_parent" 5. android:orientation="vertical" > 6. 7. <!-- 把FragmentLayout放在FragmentTabHost上面,这样tabs就在底部了,注意,id要自己添加了 8. android:id="@+id/realtabcontent" 9. --> 10. <FrameLayout 11. android:id="@+id/realtabcontent" 12. android:layout_width="match_parent" 13. android:layout_height="0dip" 14. android:layout_weight="1" /> 15. 16. <android.support.v4.app.FragmentTabHost 17. android:id="@android:id/tabhost" 18. android:layout_width="match_parent" 19. android:layout_height="150px" 20. > 21. </android.support.v4.app.FragmentTabHost> 22. 23.</LinearLayout>
下面是MainActivity.java的代码
Java代码 复制代码 收藏代码
1.import android.os.Bundle; 2.import android.support.v4.app.FragmentActivity; 3.import android.support.v4.app.FragmentTabHost; 4.import android.view.View; 5.import android.widget.LinearLayout; 6. 7.import com.dzc.talkv3.R; 8.import com.dzc.talkv3.fragment.ContactFragment; 9. 10.public class MainActivity extends FragmentActivity { 11. 12. private FragmentTabHost mTabHost; 13. 14. @Override 15. protected void onCreate(Bundle arg0) { 16. // TODO Auto-generated method stub 17. super.onCreate(arg0); 18. setContentView(R.layout.activity_main1); 19. 20. //初始化tabs 21. initTabs(); 22. } 23. 24. 25. private void initTabs(){ 26. mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); 27. mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 28. 29. //初始化4个tabs界面 30. //R.layout.main_bottom_bar_tab_msg 自己定义的tabs布局文件 一个Linearlayout里面放一个View组件就行了 31. 32. View tab_msg = getLayoutInflater().inflate(R.layout.main_bottom_bar_tab_msg, null); 33. View tab_contact = getLayoutInflater().inflate(R.layout.main_bottom_bar_tab_contact, null); 34. View tab_plugin = getLayoutInflater().inflate(R.layout.main_bottom_bar_tab_plugin, null); 35. View tab_me = getLayoutInflater().inflate(R.layout.main_bottom_bar_tab_me, null); 36. 37. //addTab(标题,跳转的Fragment,传递参数的Bundle) 38. //ContactFragment自己定义一个extends Fragment的类就行了 39. 40. mTabHost.addTab(mTabHost.newTabSpec("").setIndicator(tab_msg), ContactFragment.class, null); 41. mTabHost.addTab(mTabHost.newTabSpec("").setIndicator(tab_contact), ContactFragment.class, null); 42. mTabHost.addTab(mTabHost.newTabSpec("").setIndicator(tab_plugin), ContactFragment.class, null); 43. mTabHost.addTab(mTabHost.newTabSpec("").setIndicator(tab_me), ContactFragment.class, null); 44. //设置tabs之间的分隔线不显示 45. mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE); 46. }
相关文章推荐
- Android Studio如何修改单元测试的目录
- Android系统访问串口设备
- Android2.2平台上支持多camera(一)
- Android 开发 RxJava / RxAndroid 详解
- android 调用ZXing包实现二维码预览框图片有拉伸
- Android SDK 目录下的文件夹主要是干什么的
- android中开启子线程
- Android系统移植与平台开发(四) - led HAL简单设计案例分析
- Android系统移植与平台开发(三)- JNI介绍
- Android系统移植与平台开发(二)- HAL Stub框架分析
- Android SharedPreferences的使用
- Android冷启动白屏解析,带你一步步分析和解决问题
- 【Android】viewpager+fragment实现界面滑动,顶部导航也滑动
- Android静态安全检测 -> Broadcast Receiver组件暴露
- Android系统移植与平台开发(一)- 初识HAL
- android事件分发机制知识点
- Android动画完全解析--View动画
- Android BLE中传输数据的最大长度怎么破
- Android布局详解:FrameLayout
- Android View状态保存