之前有谈过一篇关于 layout_gravity和gravity的区别,如下:
http://blog.csdn.net/qq_15267341/article/details/52699907?locationNum=3
特别提到
android:layout_gravity 表示自己在父控件中的位置,注意是相对于父控件,但是在实际使用过程中遇到如下问题:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">"
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="静态创建"
android:layout_gravity="bottom">
</Button>
<Button
android:id="@+id/btnd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="动态创建"
android:layout_gravity="bottom">
</Button>
</LinearLayout>
上面代码布局效果如下:
按理来讲,layout_gravity 既然表示自己在父控件中的位置,那么上面代码中设置了bottom,那么应该放在下面,但是发现layout_gravity 并没有起作用,总结如下:
当父控件 android:orientation设置成vertical时,layout_gravity只有水平方向设置才起作用,如水平方向的left ,center ,right
当父控件 android:orientation设置成horizontal时,layout_gravity只有垂直方向设置才起作用,如垂直方向的top,center,bottom
FR:海涛高软(QQ技术交流群:386476712)