且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何对齐文本的Andr​​oid垂直居中

更新时间:2023-11-11 14:38:04

您的TextView属性需要是这样的,

Your TextView Attributes need to be something like,

<TextView ... 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|right" ../>

现在,说明为什么要做这些需求,

Now, Description why these need to be done,

 android:layout_width="match_parent"
 android:layout_height="match_parent"

让你的TextView到 match_parent FILL_PARENT 如果你不希望它像, match_parent 你必须给一些指定的值 layout_height 因此获得垂直重心的空间。 Android的:layout_width =match_parent必要的,因为它对准右侧的TextView的,所以你可以识别对于TextView中的父布局

Makes your TextView to match_parent or fill_parent if You don't want to be it like, match_parent you have to give some specified values to layout_height so it get space for vertical center gravity. android:layout_width="match_parent" necessary because it align your TextView in Right side so you can recognize respect to Parent Layout of TextView.

现在,其对安卓重力这使得你的TextView的调整内容。 安卓layout_gravity 使的TextView对齐尊重其父布局。

Now, its about android:gravity which makes the content of Your TextView alignment. android:layout_gravity makes alignment of TextView respected to its Parent Layout.

更新:

正如下面的评论说,使用 FILL_PARENT ,而不是 match_parent 的。 (问题的一些设备。)

As below comment says use fill_parent instead of match_parent. (Problem in some device.)