android学习之LinearLayout
LinearLayout常用属性:
android:orientation=”vertival”
——该属性决定它子类空间的排列方式(vertical:垂直;horizontal:水平)
android:gravity=”center”
——该属性决定它子类XY的位置,要使用多个位置属性可以使用“|”隔开,如“center_horizontal | bottom”
Note:android:gravity与android:layout_gravity不一样!android:gravity用在父类控件来控制子类控件的布局,android:layout_gravity用于子类控件中来表示本身在父类控件中的位置,两种一般只使用一个。
同时,在子类控件中使用gravity表示子类控件文本的位置(此时其实也可以把文本看作是此控件的子类控件)
gravity常用的属性值有:
center_vertical:垂直(Y轴)居中
center_horizontal:水平(X轴)居中
center:水平垂直都居中
right:子类控件位于当前控件的右边
left:子类控件位于当前控件的左边
bottom:子类控件位于当前布局的下面
子类控件的属性:
android:layout_gravity:略(与gravity相同)
android:layout_weight=“1”指本身控件占当前父类容器的一个比例
Note:使用android:layout_weight时要注意上下排列时height属性要用wrap_content,左右排列时weight属性要使用wrap_content,否则比例很相反
示例:
<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_weight="2" android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <Button android:layout_weight="1" android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" />LinearLayout>
结果:
把android:layout_height改成match_parent
<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_weight="2" android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button1" /> <Button android:layout_weight="1" android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button2" />LinearLayout>
结果:
更多相关文章
- RecylcerView中的子view matchParent属性失效的问题
- 自定义控件其实很简单2/3
- android安装后控件拖不动问题解答
- 【Android】Android中 Paint 字体、粗细等属性的一些设置
- 安卓笔记:安卓控件属性大全
- android layout属性 .
- Android 常用控件(三)学习笔记