FragmentTabHost+TabWidget+FrameLayout布局问题

研究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

学习完,下一篇文章再介绍。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值