2011-09-18 77 views
8

我有一個基本問題。在許多tabhost示例中,我們找到帶有圖像和文本的選項卡。如何在tabhost中居中文本?

在我的情況下,我只想顯示文本,但問題是我的文本是水平居中但不垂直(文本位於我的選項卡的底部)。

我試過了:android:layout_gravity =「center」在framelayout中,但它不起作用。

您有什麼想法嗎?

我的xml。

<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:layout_gravity="center"> 

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

     <TabWidget android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"/> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"> 

     </FrameLayout> 

    </LinearLayout> 

</TabHost> 

解決:我定製了自己的標籤感謝以下教程:http://joshclemm.com/blog/?p=136

謝謝

+0

它更好地使用自定義選項卡作爲默認選項卡具有圖標和文本的大小。 –

+0

可能重複的[如何居中對齊Android中的標籤欄中的文本](http://stackoverflow.com/questions/5164443/how-to-center-align-text-in-a-tab-bar-in- android) – user7116

+0

很久以前問過同樣的問題。請閱讀此問題[請在此輸入鏈接描述](http://stackoverflow.com/questions/5164443/how-to-center-align-text-in-a-tab-bar-in-android) – Dimon

回答

0
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center_vertical|center_horizontal" 
android:layout_gravity="center_vertical|center_horizontal" 
+0

我試過你的解決方案,但它不工作。是在我的標籤的底部。它是否適合你? – Miroufle

+0

更新了迴應,你試過嗎? – user634545

+0

是的,我試過兩個版本,但它不工作:( – Miroufle

0

使用下面的代碼,使標籤文本中心:

的RelativeLayout .LayoutParams param = new

RelativeLayout.Layout PARAMS(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

param.addRule(RelativeLayout.CENTER_IN_PARENT); 

TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0) 
      .findViewById(android.R.id.title); // Unselected Tabs 
    tv.setTextColor(Color.parseColor("#505050")); 
    tv.setTextSize(10); 
    tv.setLayoutParams(param); 
3
  1. 嘗試先獲取標籤的標題,並如下圖所示設置重力和佈局明確:

    TextView textView = (TextView)tabHost.getTabWidget().getChildAt(0) 
    .findViewById(android.R.id.title); 
    textView.setGravity(Gravity.CENTER);   
    textView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; 
    textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT; 
    

    希望這有助於。