There are 2 ways to set the center of TextView text:
One: In the XML file settings: android:gravity= "center"
II: In the program set: M_txttitle.setgravity (Gravity.center);
Note: The difference between android:gravity and Android:layout_gravity is that the former operates inside the control, and the latter is the entire control.
For example:
The code is as follows |
Copy Code |
Android:gravity= "Center" is centered on the text in TextView Android:layout_gravity= "Center" is centered on the TextView control throughout the layout |
In fact, it's easy to understand that "layout" is the control's action on the entire layout
TextView text is centered vertically on the left,
Set android:gravity= "Center_vertical|left".
android:gravity= "center", vertically horizontally
LinearLayout has two very similar properties: Android:gravity and Android:layout_gravity. The difference is that android:gravity is used to set the alignment of the view component, and android:layout_gravity is used to set the alignment of the container component.
For example, we can set android:gravity= "center" to center the text in the EditText in the EditText component, and we set the EditText android:layout_gravity= " Right "to have the EditText component centered in the LinearLayout.
code is as follows |
copy code |
< TextView android:layout_width= "fill_parent" android:layout_height= " Wrap_content " android:textsize=" 40sp " android:gravity=" Center_ Vertical|left " android:text=" @string/hello_world "/> |