一.核心属性
- 常用属性
- id 为该组件设置一个唯一资源Id,在java文件中可以通过findViewById(id)找到该组件
- layout_width 布局的宽度,通常不直接写数字的,用warp_content (组件自适应大小),match_parent填满父容器
- layout_height 布局的高度,参数同上
- orientation 布局中组件的排列方式,有horizontal(水平),vertical(竖直),两种方式
- gravity 控制组件所包含的子元素的对齐方式,可多个组合,如(left|buttom)
- layout_gravity 控制该组件在父容器里的对齐方式
- background 为改组件设置一个背景图片,背景色
- divider 分割线
- divider 为LinearLayout设置分割线的图片
- showDividers 设置分割线所在的位置,有四个值可选:none,middle,begining,end
- dividerPadding 设置分割线的padding
- Weight权重
- weightSum 设置该LinearLayout权重的最大值
- layout_weight 权重值
二.weightSum和Layout_weight属性详解
- layout_width 或者layout_height设置为0dp
按比例划分:水平方向的布局则将layout_width设置为0dp,竖直方向的布局则将Layout_height设置为0dp。 - LinearLayout 设置为warp_content
按照比例划分:将LinearLayout的layout_width或者layout_height设置为warp_content. - Linearlayout 设置为match_parent
这种情况就比较复杂一点,需要进行计算
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="three"
android:background="#FF00FF"
/>
</LinearLayout>
显示效果如下:
这个时候就会有疑问了,怎么会这样,这比例是2:1吧,那么three去哪了?代码里面明明有 three的啊,还设置了3的,而1和2的比例也不对耶,1:2:3却变成了2:1:0,怎么会这样呢? 答:这里其实没那么简单的,还是需要我们计算的,网上给出的算法有几种,这里就给出笔者 觉得比较容易理解的一种: step 1:个个都是fill_parent,但是屏幕只有一个啦,那么1 - 3 = - 2 fill_parent step 2:依次比例是1/6,2/6,3/6 step 3:先到先得,先分给one,计算: 1 - 2 * (1/6) = 2/3 fill_parent 接着到two,计算: 1 - 2 * (2/6) = 1/3 fill_parent 最后到three,计算 1 - 2 * (3/6) = 0 fill_parent step 4:所以最后的结果是:one占了两份,two占了一份,three什么都木有 以上就是为什么three没有出现的原因了,或许大家看完还是有点蒙,没事,我们举多几个例子试试就知道了!
比例为:1:1:1
结果是:1/3 1/3 1/3
比例为:2:3:4
计算结果:5/9 3/9 1/9,对比效果图,5:3:1
4. 在java代码中添加weight属性
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1));
5 . 灵活运用weightSum属性和layout_weight属性
weightSum属性定义weight总和的最大值。如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值。典型的案例是:通过指定子视图的layout_weight属性为0.5,并设置LinearLayout的weightSum属性为1.0,实现子视图占据可用宽度的50%。效果图如下:
三.为LinearLayout设置分割线
使用LinearLayout的一个divider属性,直接为LinearLayout设置分割线 这里就需要你自己准备一张线的图片了(目前只支持图片)
1. android:divider设置作为分割线的图片
2. android:showDividers设置分割线的位置,none(无),begining(开始),end(结束),middle(每两个组件间)
3. dividerPadding设置分割线的Padding
使用示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/ktv_line_div"
android:orientation="vertical"
android:showDividers="middle"
android:dividerPadding="10dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3" />
</LinearLayout>
四.注意事项
使用Layout_gravity的一个很重要的问题!!! 问题内容: 在一个LinearLayout的水平方向中布置两个TextView,想让一个左,一个右,怎么搞? 或许你会脱口而出:”gravity设置一个left,一个right就可以啦!” 真的这么简单?你试过吗?写个简单的Layout你就会发现,事与愿违。
当 android:orientation=”vertical” 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。 即:left,right,center_horizontal 是生效的。 当 android:orientation=”horizontal” 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。 即:top,bottom,center_vertical 是生效的。