2012-08-10 32 views
12

Tôi có một máy chủ lưu trữ tab trong đó tôi có nhiều hơn 6 nút. Tuy nhiên màn hình chỉ hiển thị 3-4 nút.Thêm cuộn trong tabhost trong android

Sau đây là mã mà tôi đang sử dụng cùng với hình ảnh.

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#777777"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 

     > 
     <RelativeLayout 
      android:id="@+id/layTab" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:background="@drawable/navbar_background" 
      android:layout_alignParentBottom="true" 
      android:layout_centerVertical="true" 
      > 
      <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      /> 
     </RelativeLayout> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_alignParentTop="true" 
      android:layout_above="@+id/layTab"/> 


    </RelativeLayout> 
</TabHost> 

enter image description here

Câu hỏi đặt ra là, làm thế nào tôi giả sử để thêm một cuộn giấy để tôi có thể di chuyển tối đa tất cả 6 nút hoặc bất kỳ thủ thuật khác.

Trân trọng

Trả lời

23

Chỉ cần tổ TabWidget bên trong một HorizontalScrollingView, như thế này:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#777777" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <RelativeLayout 
      android:id="@+id/layTab" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_centerVertical="true" 
      android:background="@drawable/navbar_background" 
      android:gravity="center" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" > 

      <HorizontalScrollView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:fillViewport="true" 
       android:scrollbars="none" > 

       <TabWidget 
        android:id="@android:id/tabs" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 
       </TabWidget> 
      </HorizontalScrollView> 
     </RelativeLayout> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_above="@+id/layTab" 
      android:layout_alignParentTop="true" /> 
    </RelativeLayout> 

</TabHost> 

Bằng cách này, giải pháp này làm việc cho nhiều kịch bản khác, RadioGroup, ví dụ

+0

không làm việc cho điều này, tôi không cần nhóm radio: d –

+0

Vâng, tôi đã làm sai. TabWidget cần được lồng nhau ... Tôi đã chỉnh sửa câu trả lời của mình. Hãy thử nó ngay bây giờ –

+0

Nó cũng cần thiết để lồng các HorizontalScrollView bên trong hai RelativeLayouts? Làm tổ của tôi như sau: Tương đối> TabHost> Tuyến tính> Ngang> TabWidget và nó không cuộn. Bạn có nghĩ API có thể đã thay đổi kể từ năm 2012 không? – James

Các vấn đề liên quan