The text in the TextView
defaults to left alignment of the text. How to align text across the entire width, as if in Word you pressed Ctrl+J
(the text is distributed evenly between the left and right edges of the page) ???
3 answers
gravity
for TextView
on center
should help.
android:gravity="center"
Android does not know how to align the text in the TextView
on both sides. Use either WebView
, or third-party libraries such as TextJustify-Android .
The location of the contents within its container is responsible for the gravity
parameter. In order for the content to be placed in the middle of the container, you must specify layout:gravity="center"
. However, this will work if the element occupies the entire width of its parent container, i.e. if android:layout_width="match_parent"
. If the length is set to wrap_content
, then it is necessary to set the parameters of the location of elements depending on the specific ViewGroup
. For example, for RelativeLayout
you need to set the android:layout_centerInVertical="true"
parameter for the elements inside it android:layout_centerInVertical="true"
.
- The question is different, it is necessary to align in width, not in the center - Dmitry Alexandrov