Android TabWidget底部显示

TabHost控件默认使用LinearLayout包裹TabWidget和FrameLayout,布局文件如下:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@android:id/tabhost"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent"  
  9.         android:orientation="vertical" >  
  10.   
  11.         <TabWidget  
  12.             android:id="@android:id/tabs"  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="wrap_content" >  
  15.         </TabWidget>  
  16.   
  17.         <FrameLayout  
  18.             android:id="@android:id/tabcontent"  
  19.             android:layout_width="match_parent"  
  20.             android:layout_height="match_parent" >  
  21.         </FrameLayout>  
  22.     </LinearLayout>  
  23. </TabHost>  

这样TabWidget显示在顶部,如果想把TabWidget放到底部有三种方式。

方式一:将TabHost中默认的LinearLayout换成RelativeLayout,并给TabWidget添加Android:layout_alignParentBottom="true"

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/tabhost"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:layout_alignParentLeft="true"  
  6.     android:layout_alignParentTop="true" >  
  7.   <RelativeLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent">  
  10.   
  11.         <TabWidget  
  12.             android:id="@android:id/tabs"  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="wrap_content"   
  15.             android:layout_alignParentBottom="true">  
  16.         </TabWidget>  
  17.           
  18.         <FrameLayout  
  19.             android:id="@android:id/tabcontent"  
  20.             android:layout_width="match_parent"  
  21.             android:layout_height="match_parent" >  
  22.         </FrameLayout>  
  23.     </RelativeLayout>  
  24. </TabHost>  

方式二:1、将LinearLayout中TabWidget和FrameLayout交换位置
              2、设置FrameLayout的属性:android:layout_weight="1" android:layout_height="0dp"

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <p><TabHost xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"  
  2.     android:id="@+id/tabhost"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:layout_alignParentLeft="true"  
  6.     android:layout_alignParentTop="true" ></p><p>   <LinearLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent"  
  9.         android:orientation="vertical" >  
  10.    
  11.         <FrameLayout  
  12.             android:id="@android:id/tabcontent"  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="0dp"  
  15.             android:layout_weight="1" >  
  16.         </FrameLayout>  
  17.           
  18.         <TabWidget  
  19.             android:id="@android:id/tabs"  
  20.             android:layout_width="match_parent"  
  21.             android:layout_height="wrap_content"   
  22.             android:layout_alignParentBottom="true">  
  23.         </TabWidget>  
  24.     </LinearLayout>  
  25. </TabHost></p>  

方式三:1、将TabWidget移动到LinearLayout标签以下
              2、在FrameLayout中加入属性:android:layout_gravity="top"
              3、在TabWidget中加入属性:android:layout_gravity="bottom"

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/tabhost"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:layout_alignParentLeft="true"  
  6.     android:layout_alignParentTop="true" >  
  7.   
  8.    <LinearLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:orientation="vertical" >  
  12.    
  13.         <FrameLayout  
  14.             android:id="@android:id/tabcontent"  
  15.             android:layout_width="match_parent"  
  16.             android:layout_height="match_parent"  
  17.             android:layout_gravity="top" >  
  18.         </FrameLayout>  
  19.     </LinearLayout>  
  20.     <TabWidget  
  21.         android:id="@android:id/tabs"  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"   
  24.         android:layout_gravity="bottom">  
  25.     </TabWidget>  
  26. </TabHost>  

以上三种方式在Android4.2下测试通过。

原文地址:https://www.cnblogs.com/pangguoming/p/6164827.html