Android学习|布局—— LinearLayout
一、常见属性
1、orientation:布局中组件的排列方式。
vertical为纵向排列、horizontal为水平排列
2.、gravity : 控制组件所包含的子元素的对齐方式,可多个组合
3、layout_gravity: 控制该组件在父容器里的方式:
top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、
fill_horizontal、center、fill、clip_vertical、clip_horizontal、start、end
4、background:为该组件设置一个背景图片,或者是直接用颜色覆盖
5、divider:分割线
6、showDividers:设置分割线所在的位置
none(无), beginning(开始),end(结束),middle(每两个组件间)
divider设置后还需要设置showDividers才能生效
7、dividerPadding 设置分割线的 padding边距
8、layout_weight(权重):该属性是用来等比例的划分区域
权重设置是在原有的height基本上,将屏幕剩余的部分按照权重进行分配
当height设置成match_parent即自适应屏幕时,权重设置会产生变化,需要具体计算
所以带权重进行比例设置时,最好layout_height设为0dp,然后进行layout_weight的权重比例设置
二、练习
1、orientation
<?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:orientation="vertical">
<!-- android:orientation="vertical" 看布局的排列方式: vertical 纵向排列 、 horizontal 水平排列-->
<!--layout_gravity练习-->
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#7F5454">
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:background="#428BD8">
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="fill_vertical"
android:background="#8FB659">
</LinearLayout>
</LinearLayout>
vertical:
horizontal:
2、layout_height
控制组件所包含的子元素的对齐方式,可多个组合
<?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