Horizontally and Vertically center text for TextView in Android

情到浓时终转凉″ 提交于 2019-12-23 05:13:45

问题


How do I center the text horizontally and vertically for a TextView in Android? How do I center the alignment of a TextView?


回答1:


In Linear Layouts use

android:layout_gravity="center"

In Relative Layouts

android:layout_centerInParent="true"

Above Code will center the textView in layout To center the text in TextView

android:textAlignment="center"
android:gravity="center"



回答2:


add this property for textview in xml

textview<android:height="yourheight"
android:width="yourwidth"
android:gravity="center">

your width size should be less than the parent container's width size




回答3:


It depends on how you have declared the height and width. If you used wrap_content, then the textview is just big enough in that direction to hold the text, so the gravity attribute will not apply. android:gravity affects the location of the text Within the space allocated for the textview. layout_xx attributes affect the location of the view within it's parent.

SO if you set the size of the textview, then use gravity to position the text within the textview area, if you used wrap_content, then use the layout_xx attributes to position the textview within it's parent



来源:https://stackoverflow.com/questions/21942349/horizontally-and-vertically-center-text-for-textview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!