安卓 layout_gravity 使用时注意

官网定义了 layout_gravity的使用方法。意思是用于控制子view在父view中的显示位置:


Standard gravity constant that a child supplies to its parent. Defines how the child view should be positioned, on both the X and Y axes, within its enclosing layout.

Must be one or more (separated by ‘|‘) of the following constant values.


http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_gravity


so,写了一个代码。


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >


    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="hahha"
        android:layout_gravity="center"
>

</LinearLayout>

期待着按钮能够居中,而实际上。。

安卓 layout_gravity 使用时注意

当然要使这个按钮居中可以在LinearLayout中设置android:gravity="center"


总结:


1、并不是所有的属性值在任何条件下都起作用

2、当view在竖直线性布局中时,layout_gravity属性只是在水平方向上才会起作用。



安卓 layout_gravity 使用时注意,布布扣,bubuko.com

安卓 layout_gravity 使用时注意

上一篇:手机键盘


下一篇:【android自定义控件】button样式自定义