在布局中水平居中对齐两个文本视图 [英] Horizontally center align two textviews in a layout

查看:115
本文介绍了在布局中水平居中对齐两个文本视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在屏幕中央水平对齐两个textview.两者textviews的字体大小都不同.

I need to horizontally align two textview in the center of the screen. Both textviews have different font size.

这是我的代码:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top|center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/progressstatus"
        android:layout_gravity="center_horizontal"
        android:textColor="#FFFFFF"
        android:textSize="50sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/percent"
        android:textColor="#CCCCCC"
        android:textSize="20sp" />

</LinearLayout>

现在我的文本视图向左对齐,并且都显示相同的字体大小.

Right now my textviews are aligned left and both are showing same font size.

推荐答案

尝试一下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/progressstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical|right"
        android:text="75"
        android:textColor="#FFFF00"
        android:textSize="50sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="%"
        android:textColor="#CCCCCC"
        android:textSize="20sp" />
</LinearLayout>

说明:我将父级布局设置为wrap_content,这意味着父级将根据子级的高度包装其高度.父级中有2个textview.一种具有较大的字体大小,而另一种具有较小的字体大小.较大的字体大小视图肯定会比较小的高度更大.因此,我将较大的高度设置为wrap_content.现在,较小的视图的高度为match_parent,这意味着较小的视图将自身扩展到父级的最大高度,该高度等于较大字体的视图的高度.因此,两者都将居中对齐.

Explanation: I set the parent layout to wrap_content, means parent will wrapped its height according to child heights. There are 2 textviews in parent. One has bigger font size and other has smaller font size. Bigger font size view would definitely has more height then smaller one. So, i set the bigger height to wrap_content. Now, the smaller view's height is match_parent, means smaller one would expand itself to max height of parent that would be equal to the height of bigger font's view.. So, both would be center_aligned.

这篇关于在布局中水平居中对齐两个文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆