Android中RelativeLayout的字符水平(垂直居中)對齊


【背景】

此處Android中顯示出來的TextView中的內容,水平中間不對其。

想要實現水平居中對齊。

【折騰過程】

1.搜:

android RelativeLayout horizontal center

參考:

android – TextView horizontal center in RelativeLayout – Stack Overflow

試過了:

android:layout_centerHorizontal="true"

但沒用。

2.后來看到:

relativelayout – Android Relative Layout Align Center – Stack Overflow

去試了試:

android:layout_centerVertical="true"

才明白:

我上面是錯誤的搜了horizontal ,實際上該去搜Vertical才對。

現在效果是所需要的了:

android text horizontal center align

【總結】

想要水平居中對齊,則只需要去設置:

android:layout_centerVertical="true"

即可。

【引申】

更多的其他參數,可以去官網:

RelativeLayout.LayoutParams | Android Developers

中找到:

Attribute Name

Related Method

Description

android:layout_above

Positions the bottom edge of this view above the given anchor view ID.

android:layout_alignBaseline

Positions the baseline of this view on the baseline of the given anchor view ID.

android:layout_alignBottom

Makes the bottom edge of this view match the bottom edge of the given anchor view ID.

android:layout_alignEnd

Makes the end edge of this view match the end edge of the given anchor view ID.

android:layout_alignLeft

Makes the left edge of this view match the left edge of the given anchor view ID.

android:layout_alignParentBottom

If true, makes the bottom edge of this view match the bottom edge of the parent.

android:layout_alignParentEnd

If true, makes the end edge of this view match the end edge of the parent.

android:layout_alignParentLeft

If true, makes the left edge of this view match the left edge of the parent.

android:layout_alignParentRight

If true, makes the right edge of this view match the right edge of the parent.

android:layout_alignParentStart

If true, makes the start edge of this view match the start edge of the parent.

android:layout_alignParentTop

If true, makes the top edge of this view match the top edge of the parent.

android:layout_alignRight

Makes the right edge of this view match the right edge of the given anchor view ID.

android:layout_alignStart

Makes the start edge of this view match the start edge of the given anchor view ID.

android:layout_alignTop

Makes the top edge of this view match the top edge of the given anchor view ID.

android:layout_alignWithParentIfMissing

If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc.

android:layout_below

Positions the top edge of this view below the given anchor view ID.

android:layout_centerHorizontal

If true, centers this child horizontally within its parent.

android:layout_centerInParent

If true, centers this child horizontally and vertically within its parent.

android:layout_centerVertical

If true, centers this child vertically within its parent.

android:layout_toEndOf

Positions the start edge of this view to the end of the given anchor view ID.

android:layout_toLeftOf

Positions the right edge of this view to the left of the given anchor view ID.

android:layout_toRightOf

Positions the left edge of this view to the right of the given anchor view ID.

android:layout_toStartOf

Positions the end edge of this view to the start of the given anchor view ID.

至於每個都是什么效果,就可以自己去多試試就知道了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM