研究TabHost布局时发现,无论怎么修改TabWidget与FrameLayout的位置和它们的属性,都没法将TabWidget放在底部,包括设置TabWidget属性
android:layout_alignParentBottom="true"
android:tabStripEnabled="false"
无奈始终停留在顶部,这是放在顶部的布局文件
<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" >
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- android:background="#b2b2b2" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- android:background="@layout/shape" -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
里面再添加LinearLayout布局能勉强保证内容正常显示,FrameLayout的内容不会与TabWidget重合,不会像这样
有热心网友提供了两种方法,将TabWidget放在底部
实现方式一:通过TabWidget实现
这种方式主要是在布局中将TabWidget标签嵌套在RelativeLayout中,并且在TabWidget标签中中设置 android:layout_alignParentBottom="true"
另外,下划线和选项卡之间的线去除的方法时在TabWidget标签中设置属性android:tabStripEnabled="false"
布局文件activity_main.xml
<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" >
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- tabStripEnabled属性去掉底部下划线与选项卡间的下划线 -->
<!-- layout_alignParentBottom属性即可将其放在底部菜单栏,注意,必须在RelativeLayout里 -->
<TabWidget
android:id="@android:id/tabs"
android:tabStripEnabled="false"
android:background="#6E6E6E"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
></TabWidget>
</RelativeLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
实现的关键是将TabWidget放在相对布局中,并设置属性
android:layout_alignParentBottom="true"
界面效果如下:
实现方式二:隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏
这种方式更漂亮,网上基本用的是这种方式,通过setCurrentTabByTag来切换不同的选项卡
main.xml
学习完,下一篇文章再介绍。