【问题标题】:How to center text in tabhost?如何在tabhost中居中文本?
【发布时间】:2011-11-19 15:45:48
【问题描述】:

我有一个基本问题。在许多 tabhost 示例中,我们可以找到带有图像和文本的选项卡。

在我的情况下,我只想显示一个文本,但问题是我的文本水平居中而不是垂直居中(文本位于标签的底部)。

我在框架布局中尝试了:android:layout_gravity="center",但它不起作用。

请问你有什么想法吗?

我的 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

谢谢

【问题讨论】:

标签: android center android-tabhost vertical-alignment


【解决方案1】:
  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;
    

    希望对您有所帮助。

【讨论】:

    【解决方案2】:

    使用下面的代码使标签文本居中:

    RelativeLayout.LayoutParams param=new

    RelativeLayout.LayoutParams(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】:
      android:layout_width="fill_parent"   
      android:layout_height="fill_parent"
      android:gravity="center_vertical|center_horizontal"
      android:layout_gravity="center_vertical|center_horizontal"
      

      【讨论】:

      • 我尝试了您的解决方案,但它不起作用。我的文字仍然在标签的底部。对你有用吗?
      猜你喜欢
      • 1970-01-01
      • 2019-08-19
      • 2014-07-21
      • 2021-01-10
      • 2014-07-09
      • 2016-01-08
      • 2021-05-17
      • 2014-09-02
      • 2014-08-08
      相关资源
      最近更新 更多