An example of using LinearLayout for centered layout in Android App
To center your control horizontally or vertically, simply set the [Android: gravity = "center"] attribute in the upper level of the control, such as:
Such an ImageView control will stay right in the middle of your selected area. There are many attribute values of gravity, which can be set horizontally or vertically. You can view the corresponding documents or use the prompt function of eclipse to view them quickly. Let's take a look at an example. If you want to implement such a layout with two buttons centered, what should you do?
The easier to implement is the LinearLayout layout. Of course, if you change to relativelayout, you can also implement it. Here's a brief description of how to realize the layout with relativelayout. First, find the center point, and then take this center point as a reference. Next, let's take a look at the implementation of the more easily implemented LinearLayout layout.
If the requirements have changed and only one button is needed in the middle, the implementation is very simple. You only need to delete one of the button tags above, but some people do so
Obviously, this implementation cannot be centered, although Android: layout is specified in the button tag_ Gravity = "center" but the layout seems ungrateful. Why? The reason is very simple. LinearLayout can only have one direction, either vertical or horizontal. It will not lead the layout at all_ Gravity is the attribute, so the above implementation has only such a result
If you change to a horizontal layout
This will happen
You can figure it out according to the above theory, so Android: Layout_ Although LinearLayout also has gravity = "center", it's a false shot. What really works is its own property Android: gravity = "center". Of course, if you want to find out why it doesn't work, you can start from the source code of LinearLayout, which will not be repeated here.