[英]How to align the text inside the TextView in Android?
我在 Android 中有一個多行 TextView。 它是 LinearLayout 的子代。 它已經水平居中,我也想讓里面的文本居中。
這是我現在所擁有的:
_ _ _ _ _ _| aaaaaaaaaaaaaaaaaaaaa
_ _ _ _ _ _| aaaaaaaa
_ _ _ _ _ _| aaaaaaaaaaaaaa
我想要的是:
_ _ _ _ _ _| aaaaaaaaaaaaaaaaaaaaa
_ _ _ _ _ _| aaaaaaaa
_ _ _ _ _ _| aaaaaaaaaaaaaa
在哪里:
_ _ _ _ = TextView 側面的空間
| = TextView 邊緣
您是否嘗試過TextView
上的gravity
屬性?
android:gravity="center"
並確保使用android:gravity
而不是android:layout_gravity
。
是的,使用android:gravity屬性來改變視圖中數據的位置。
重力屬性是對對齊的更新。 重力在屬性檢查器中可用,並且能夠做到;
android:gravity="center"
如果您只想在 textView 中對齊文本,那么textAlignment
是比重力更好的選擇。
為Center
添加android:textAlignment="center"
為Left
添加android:textAlignment="textStart"
為Right
添加android:textAlignment="textEnd"
在 strings.xml 中制作文本(要設置為 TextView),如下所示:
<string name="content">
<center>Your text goes here.</center>
</string>
並在 TextView 添加以下屬性:
<TextView
...
android:text="@string/content"/>
它會將文本水平居中放置在 TextView 中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.