Background
In Android, you want to set the horizontal alignment of a button, you are exhausted:
How to set the center alignment of a button if the Tablelayout layout has been resolved in ADT it is now necessary to figure out what the difference is between gravity and layout_gravity.
1. Reference:
Android–gravity and Layout_gravity
The difference between gravity and layout_gravity in Android
The explanations in this article can be summed up as:
- Android:gravity: Represents the current view, that is, the control, the inner thing, the alignment
- Button inside the TableRow
<tablerow android:layout_width= "fill_parent" android:layout_height= "Fill_parent" android: Gravity= "Right" > <button android:layout_width= "fill_parent" android:layout_height= "Fill_ Parent " android:gravity=" center " android:layout_gravity=" center " android:onclick=" Preformdownload " android:text=" @string/btn_download "/></tablerow>
<tablerow android:layout_width= "fill_parent" android:layout_height= "Fill_parent" android: gravity= "center" > <button android:layout_width= "fill_parent" android:layout_height= "Fill_ Parent " android:gravity=" center " android:layout_gravity=" center " android:onclick=" Preformdownload " android:text=" @string/btn_download "/> </TableRow>
- Text (text) inside the button:
<button android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:layout_ gravity= "Bottom" android:gravity= "top" android:onclick= "preformdownload" android:text= "@string/ Btn_download "/>
- Vertical: Center Alignment:
<button android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:layout_ gravity= "Bottom" android:gravity= "center_vertical" android:onclick= "Preformdownload" android:text= "@string/btn_download"/>
- Android:layout_gravity: Represents the alignment of the current view, that is, the control, itself, in the display range assigned to the control at the parent level
- EditText (within the linearlayout of the parent level)
- EditText Center Alignment:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= " Vertical " android:layout_width=" fill_parent " android:layout_height=" fill_parent "> <edittext android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= " Center " android:gravity=" center " android:text=" one "/></linearlayout>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= " Vertical " android:layout_width=" fill_parent " android:layout_height=" fill_parent "> <edittext android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= " Right " android:gravity=" center " android:text=" one "/></linearlayout>
Summary
- android:gravity : Represents the current view, that is, the control, the inner thing, the alignment
- The common ones are:
- button in the TableRow
- EditText (internal) text
- The text of the Button (inside)
- android:layout_gravity: Represents the alignment of the current view, which is the control itself , that is within the display range assigned by the parent level control to the current child control.
- The common ones are:
- The alignment of the current edittext (within the display range assigned to it by the parent level linelayout)
- The alignment of the current button (within the display range assigned to it by the parent level TableRow) Here, it is important to note that many times, changing the layout_gravity in the button often does not see the effect of the change, because its display range and position, has been determined by the gravity of the TableRow at the parent level.
In short, take the code, try more, it is easy to understand.
Gravity and layout_gravity differences in "finishing" Android