Example 1: Android: gravity = "right": Align the content of a single view to the right.
<? XML version = "1.0" encoding = "UTF-8" ?> < Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" Android: gravity = "Right" > < Button Android: ID = "@ + ID/button1" Android: layout_width = "Wrap_content" Android: layout_height = "Wrap_content" Android: Text = "Button" /> </ Linearlayout >
The button is on the right of linearlayout.
Example 2: Android: layout_gravity = "right" is right-aligned to the right of the view in the container.
<? XML version = "1.0" encoding = "UTF-8" ?> < Framelayout Xmlns: Android = "Http://schemas.android.com/apk/res/android" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" Android: Orientation = "Horizontal" > < Textview Android: ID = "@ + ID/textview1" Android: layout_width = "Wrap_content" Android: layout_height = "Wrap_content" Android: layout_gravity = "Left" Android: Text = "Textview" /> < Textview Android: ID = "@ + ID/textview2" Android: layout_width = "Wrap_content" Android: layout_height = "Wrap_content" Android: layout_gravity = "Right" Android: Text = "Textview" /> </ Framelayout >
The display result is textview1 to the left and textview2 to the right.
Note:: Framelayout is used here. If linearlayout is used,
When Android: Orientation = "horizontal", the horizontal direction is left to right.
When Android: Orientation = "vertical", it can only be horizontally aligned, and the vertical direction is from top to bottom.