preview
LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列
常用属性:
android:orientation=”“——该属性决定它子类控件的排布方式(vertical-垂直;horizontal-水平)
android:gravity=”“————–该属性决定它子类的xy的位置:
center_vertical————垂直(Y轴)居中
center_horizontal———垂直(X轴)居中
center————————水平垂直都居中
right————————–子类控件位于当前布局的右边
left—————————子类控件位于当前布局的左边
bottom———————–子类控件位于当前布局的下面
android:layout_gravity=”“———–表示本身在当前父容器的XY的一个位置
layout_gravity是想在父布局里面改变位置的时候用的,
gravity是想改变自己内部东西位置是用的
android:gravity属性是对该view中内容的限定.比如一个button 上面的text. 你可以设置该text在button内的位置.
android:layout_gravity是用来设置该view相对与父view 的位置.比如一个button 在linearlayout里,你想把该button放在linearlayout里靠左、靠右等位置就可以通过该属性设置.
android:layout_weight=”“———–指本身控件占当前父容器的一个比例
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="Button"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_weight="2" />
<Button
android:text="Button"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="Button"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button"
android:layout_weight="2" />
<Button
android:text="Button"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button1"
android:layout_weight="1" />
</LinearLayout>
这两个xml文件里面唯一区别就是一个是android:layout_height=”wrap_content”,一个是android:layout_height=”match_parent”,wrap_content
会使比例正常显示,而match_parent则会反转
exercise
无