Android Gravity Properties and Weight properties

Source: Internet
Author: User

Look at this layout file.

1 <?XML version= "1.0" encoding= "Utf-8"?>  2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"  3 android:orientation= "vertical"  4 Android:layout_width= "Fill_parent"  5 Android:layout_height= "Fill_parent"  6     >  7 <EditText8     Android:layout_width= "Fill_parent"  9 Android:layout_height= "Wrap_content"  Ten Android:text= "One"/>   One <EditText A     Android:layout_width= "Fill_parent"   - Android:layout_height= "Wrap_content"   - Android:text= "both"/>   the     <EditText -     Android:layout_width= "Fill_parent"   - Android:layout_height= "Wrap_content"   - Android:text= "three"/>   + </LinearLayout>  

This is a very normal layout file, the effect is as follows.



When we set the Gravity property to these three edittext, we look at the XML file first.

1 <?XML version= "1.0" encoding= "Utf-8"?>  2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"  3 android:orientation= "vertical"  4 Android:layout_width= "Fill_parent"  5 Android:layout_height= "Fill_parent"  6     >  7 <EditText8     Android:layout_width= "Fill_parent"  9 Android:layout_height= "Wrap_content"  Ten android:gravity= "Left"   One Android:text= "One"/>   A <EditText -     Android:layout_width= "Fill_parent"   - Android:layout_height= "Wrap_content"   the android:gravity= "Center"   - Android:text= "both"/>   -     <EditText -     Android:layout_width= "Fill_parent"   + Android:layout_height= "Wrap_content"   - android:gravity= "Right"   + Android:text= "three"/>   A </LinearLayout>  



From this you can see that the gravity property is used to control the position of the text inside the edittext.

We now make the following modifications to the XML file

1 <?XML version= "1.0" encoding= "Utf-8"?>  2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"  3 android:orientation= "vertical"  4 Android:layout_width= "Fill_parent"  5 Android:layout_height= "Fill_parent"  6     >  7 <EditText8     Android:layout_width= "Fill_parent"  9 Android:layout_height= "Wrap_content"  Ten android:gravity= "Left"   One Android:text= "One"/>   A <EditText -     Android:layout_width= "Fill_parent"   - Android:layout_height= "Wrap_content"   the android:gravity= "Center"   - Android:layout_weight= "1.0"   - Android:text= "both"/>   -     <EditText +     Android:layout_width= "Fill_parent"   - Android:layout_height= "Wrap_content"   + android:gravity= "Right"   A Android:text= "three"/>   at </LinearLayout> 

Run the following effect



It feels like this android:layout_weight= "1.0" is used to set the size of the control, because the edittext in the middle of our setup gets bigger. Other two edittext of the Android:layout_weight property we do not have a setting, the default value is used without setting, and the default value is 0.0. The control that is set to 1.0 fills the remaining blank portion.
If we want 3 components to share the space equally, we should set their weight property to 1.0 so that each edittext will be extended evenly.

Android Gravity Properties and Weight properties

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.