一.我们屏幕适配的使用的比较多的就是LinearLayout的weight(权重属性)
1.常用属性
属性 | 使用 |
---|---|
orientation | 布局中组件的排列方式,有horizontal(水平),vertical(竖直,默认) |
gravity | 控制组件所包含的子元素的对齐方式,可多个组合,如(left,buttom) |
layout_gravity | 控制该组件在父容器里的对其方式 |
layout_width | 布局的宽度,通常不直接写数字的,用wrap_content(组件实际大小),fill_parent或者match_parent填满父容器 |
layout_height | 布局的高度,通常不直接写数字的,用wrap_content(组件实际大小),fill_parent或者match_parent填满父容器 |
id | 为该组件设置一个资源id,在java文件中可以通过findViewById(id)找到该组件 |
background | 为该组件设置一个背景图片,或者直接哟弄颜色覆盖 |
2.Weight(权重):用来等比例划分区域
最简单的用法:要等比例划分,分谁,谁为0,weight按比例即可
3.divider分割线:该属性用于为LinearLayout设置分割线图片,通过showDividers来设置分割线的所在位置,有四个可选值none,middle,begining,end
(1)divider:为LinearLayout设置分割线的图片
(2)showDividers:设置分割线所在的位置,有四个可选值:none,middle,begining,end
(3)dividerPadding:设置分割线的padding
二.weight(权重)属性详解
1.最简单的用法
实现代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/myActionBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="@color/colorpaper"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="@color/colorgreen"
android:layout_weight="2"/>
</LinearLayout>
要实现第一个的1:1的效果,只需要分别把两个LinearLayout的weight改成1和1就可以了
用法归纳: 按比例划分水平方向:将涉及到的View的android:width属性设置为0dp,然后设置为android weight属性设置比例即可;竖直方向,只需设android:height为0dp,然后设weight属性即可
2.weight属性详解
当然,如果我们不适用上述那种设置为0dp的方式,直接用wrap_content和match_parent的话, 则要接着解析weight属性了,分为两种情况,wrap_content与match_parent,另外还要看 LinearLayout的orientation是水平还是竖直,这个决定哪个方向等比例划分
(1)wrap_content比较简单,直接就按比例
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/myActionBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="one"
android:background="@color/colorgreen"/>
<TextView
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="two"
android:background="@color/coloryellowr"/>
<TextView
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="three"
android:background="@color/colorpaper"/>
</LinearLayout>
(2)match_parent(fill_parent):这个则需要计算了
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/myActionBar"
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="@color/colorgreen"/>
<TextView
android:layout_weight="2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="two"
android:background="@color/coloryellowr"/>
<TextView
android:layout_weight="3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="three"
android:background="@color/colorpaper"/>
</LinearLayout>
比例1:1:1
有疑问说three不见了,比例从1:2:3编程了2:1:0 ,这个时候就需要去计算了
个个都是fill_parent,但是屏幕只有一个啦,那么1 - 3 = - 2 fill_parent
所以最后的结果是:one占了两份,two占了一份,three什么都没有了,还懵的话我们可以多举几个例子
比例2:3:4