LinearLayout又称为线性布局,是一种非常常用的布局,这个布局会将它所包含的控件在线性方向上依次排列
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"/>
</LinearLayout>
android:orientation属性 确认LinearLayout中的控件是怎么排序的,如果是horizontal则是水平排序的,如果是vertical则是垂直排序的
如果LinearLayout的排列方式是horizontal,内部控件就绝对不能将宽度指定为match_parent,因为这样,单独的一个控件就将整个水平方向占满,其他控件就没有位置可以放了.同样的,如果LinearLayout的排列方式是vertical,内部控件就不能将高度指定为match_parent
android:Layout_gravity属性和android:gravity属性的区别
android:gravity是指定文字在控件中的对齐方式
android:Layout_gravity是用于指定控件在布局中的对齐方式
注意:
当LinearLayout的排序方式为horizontal时,只有垂直方向上的对齐方式才会生效,因为水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因此无法指定该方向上的对齐方式
当LinearLayout的排序方式为vertical时,只有水平上的对齐方式才会生效
android:Layout_weight 属性,这个属性允许我们使用比例的方式来指定控件的大小,在手机屏幕适配性方面可以起到非常重要的作用.其原理非常简单,系统会先把LinearLayout下所有控件指定的Layout_weight值相加,得到一个总值,然后每个控件所占的比例就是用该控件的Layout_weight值除以刚才算出来的总值.