2011-09-18 53 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; 
    

    希望这有助于。