Android TabWidget底部显示

时间:2023-03-08 16:43:19

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

  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. <LinearLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical" >
  9. <TabWidget
  10. android:id="@android:id/tabs"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content" >
  13. </TabWidget>
  14. <FrameLayout
  15. android:id="@android:id/tabcontent"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent" >
  18. </FrameLayout>
  19. </LinearLayout>
  20. </TabHost>

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

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

  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. <TabWidget
  11. android:id="@android:id/tabs"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentBottom="true">
  15. </TabWidget>
  16. <FrameLayout
  17. android:id="@android:id/tabcontent"
  18. android:layout_width="match_parent"
  19. android:layout_height="match_parent" >
  20. </FrameLayout>
  21. </RelativeLayout>
  22. </TabHost>

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

  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. <FrameLayout
  11. android:id="@android:id/tabcontent"
  12. android:layout_width="match_parent"
  13. android:layout_height="0dp"
  14. android:layout_weight="1" >
  15. </FrameLayout>
  16. <TabWidget
  17. android:id="@android:id/tabs"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_alignParentBottom="true">
  21. </TabWidget>
  22. </LinearLayout>
  23. </TabHost></p>

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

  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. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:orientation="vertical" >
  11. <FrameLayout
  12. android:id="@android:id/tabcontent"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:layout_gravity="top" >
  16. </FrameLayout>
  17. </LinearLayout>
  18. <TabWidget
  19. android:id="@android:id/tabs"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_gravity="bottom">
  23. </TabWidget>
  24. </TabHost>

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