Android TextView Text Center and vertical left center

Source: Internet
Author: User

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 "/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.