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