2013-03-13 33 views
1

我試過的解決方案:如何在xml中設置tabwidget中tab的高度?

mTabHost.getTabWidget().getChildAt(i).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,100)); 

,但我想這樣做的XML。

這是我的佈局:

<android.support.v4.app.FragmentTabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginRight="10dp" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       style="@style/tab" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0" 
       android:orientation="horizontal" 
       android:textColor="@drawable/tab_text_selector" /> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_weight="0" /> 

      <FrameLayout 
       android:id="@+android:id/realtabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" /> 
     </LinearLayout> 
    </android.support.v4.app.FragmentTabHost> 

我試圖設置TabWidget,但沒有結果的高度。

我使用自定義視圖爲單選項卡:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView android:id="@+id/img_tab" 
     android:layout_height="30dp" 
     android:layout_width="30dp" 
     android:scaleType="centerCrop" 
     android:layout_alignParentLeft="true" 
     android:layout_margin="5dp" 
    /> 

    <TextView 
     android:id="@+id/txv_tab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:textSize="22sp" 
     android:textAllCaps="true" 
     android:textIsSelectable="false" 
     android:layout_toRightOf="@+id/img_tab" 
     android:layout_margin="5dp"> 
    </TextView> 

    <View android:id="@+id/tab_border" 
     android:layout_width="match_parent" 
     android:layout_height="5dp" 
     android:layout_alignParentBottom="true" 
     android:background="@android:color/holo_green_light" 
     /> 

</RelativeLayout> 

我試圖設置RelativeLayout的高度,但每次我全屏高度的標籤的時間。

如何設置高度?

回答

3

我不認爲有可能在xml中的android tabwidget中設置標籤高度。我從來沒有找到辦法做到這一點。 爲了避免不良影響的問題,我創建了一個非常簡單的類是這樣的:

public class TipoDisp 
{ 

    public static int alt_tabs(Context cont) 
    { 
    int alt; 
    int dx, dy; 
    DisplayMetrics metrics = cont.getResources().getDisplayMetrics(); 


    dx = metrics.widthPixels; 
    dy = metrics.heightPixels; 
    if (dx < dy) 
     alt = dy/15; 
    else 
     alt = dy/10; 

    return alt; 
    } 
} 

,然後,在活動中我用的是這樣的:

int alt = TipoDisp.alt_tabs(myactivity.this); 
    mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = alt; 
    ..... 

希望這有助於

+1

我標記這是答案,因爲在互聯網上搜索沒有辦法做我想做的事,但你的是我發現的唯一途徑。 – gdantimi 2013-05-27 14:24:34

0

假設我們要設置第一個標籤的高度,那麼你需要做的是:

mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height=100; 

mTab​​Host =它是我自己的tabhost。將您的tabhost名稱放在那裏 我設置了高度100dp。現在您根據您的要求進行更改。謝謝

相關問題