Android 对Layout_weight属性…
来源:互联网 发布:盗取数据 编辑:程序博客网 时间:2024/06/06 22:05
找了很久,就下面这篇比较靠谱
今天主要说的是对Layout_weight属性的完全解析,以及利用Layout_weight这个属性使用ListView来实现表格的效果,我们都知道Android里面专门有一个TableLayout来实现表格的,说实话,我平常开发中用TableLayout还是比较少的,几乎没有用到,我们完全可以用LinearLayout和RelativeLayout来代替TableLayout的使用,自己开发中主要使用LinearLayout,RelativeLayout这两种布局,不过刚开始我还是偏爱于RelativeLayout,因为在RelativeLayout里面我们可以直接拖拽控件来布局,比较方便,现在对这两种布局偏爱各半吧,LinearLayout里面有一个属性android:layout_weight比较重要,我们在开发中常常使用它来调节界面效果,也行很多人还不了解这个属性的使用,不过没关系,我首先先带大家理解android:layout_weight属性然后在利用它来实现一个表格效果
android:layout_weight是指LinearLayout先给里面的控件分配完大小之后剩余空间的权重,也许你暂时还是摸不到头脑,不过没有关系,下面我通过例子来解释layout_weight到底是什么意思,先看下面的布局文件,一个LinearLayout,里面3个文本框
- <</SPAN>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="horizontal" > -
-
<</SPAN>TextView -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:background="#0045f5" -
android:gravity="center" -
android:text="1" /> -
-
<</SPAN>TextView -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:background="#00ff47" -
android:gravity="center" -
android:text="2" -
android:layout_weight="1"/> -
-
<</SPAN>TextView -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:background="#ff5600" -
android:gravity="center" -
android:layout_weight="1" -
android:text="3" /> -
- </</SPAN>LinearLayout>
为什么效果是这个样子呢,首先3个文本框的宽度都是“wrap_content”,根据视图内部内容自动扩展,LinearLayout就先给3个TextView分配空间适当的空间大小,假设为每个TextView分配10dip的宽度,屏幕的宽度为480dip,那么LinearLayout的剩余空间就是 480 - 3*10 =450dip,由于第一个TextView没有设置layout_weight,所以它的宽度就是10dip,而后面两个TextView设置layout_weight都是1,所以后面两个TextView就平均分配LinearLayout的剩余空间,即为450 / 2
如果我们实际开发中,你设置里面控件的宽度为”wrap_content“,然后想让里面的控件按比例占用大小,那么你就大错特错了,为什么呢?我们看看下面的代码
- <</SPAN>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="horizontal" > -
-
<</SPAN>TextView -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:background="#0045f5" -
android:gravity="center" -
android:text="1" /> -
-
<</SPAN>TextView -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:background="#00ff47" -
android:gravity="center" -
android:text="2222222222222222222" -
android:layout_weight="1"/> -
-
<</SPAN>TextView -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:background="#ff5600" -
android:gravity="center" -
android:layout_weight="1" -
android:text="3" /> - </</SPAN>LinearLayout>
你本来想让后面两个TextView平均分配剩余控件,可是下面的效果却并不是你想要的,如下图
其实因为3个TextView的宽度都是”wrap_content“,LinearLayout会先按照TextView里面的内容分配好大小,由于第2个TextView内容很多,所以LinearLayout为其分配更多的空间,使得剩余空间变小了,原理和上面的一样,那么我们在实际开发中要怎么设置按比例分配呢。知道原理其实就很简单,比如我们想要3个TextView按照1:2:3的效果
- <</SPAN>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="horizontal" > -
-
<</SPAN>TextView -
android:layout_width="0dip" -
android:layout_height="wrap_content" -
android:background="#0045f5" -
android:gravity="center" -
android:layout_weight="1" -
android:text="1" /> -
-
<</SPAN>TextView -
android:layout_width="0dip" -
android:layout_height="wrap_content" -
android:background="#00ff47" -
android:gravity="center" -
android:text="2222222222222222222" -
android:layout_weight="2"/> -
-
<</SPAN>TextView -
android:layout_width="0dip" -
android:layout_height="wrap_content" -
android:background="#ff5600" -
android:gravity="center" -
android:layout_weight="3" -
android:text="3" /> - </</SPAN>LinearLayout>
我们只需要将3个TextView的宽度设置为0dip,首先LinearLayout为3个TextView分配0dip的宽度,剩余空间就是480 - 3 * 0 = 480dip,然后剩余空间在按照权重分配,所以我们看到的效果就是1:2:3
通过上面的讲解,也许你会得出一个结论,权重越大,LinearLayout为其分配的空间就越大,我只能说这个结论下有点早了,我们继续看布局
- <</SPAN>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="horizontal" > -
-
<</SPAN>TextView -
android:layout_width="fill_parent" -
android:layout_height="wrap_content" -
android:background="#0045f5" -
android:gravity="center" -
android:layout_weight="1" -
android:text="1" /> -
-
<</SPAN>TextView -
android:layout_width="fill_parent" -
android:layout_height="wrap_content" -
android:background="#00ff47" -
android:gravity="center" -
android:text="2" -
android:layout_weight="2"/> -
-
<</SPAN>TextView -
android:layout_width="fill_parent" -
android:layout_height="wrap_content" -
android:background="#ff5600" -
android:gravity="center" -
android:layout_weight="2" -
android:text="3" /> - </</SPAN>LinearLayout>
也许你会很纳闷,怎么不是你想要的1:2:2的效果,我来为你解决疑惑吧,原理跟上面的还是一样的,因为我们这里为每个TextView设置的宽度为”fill_parent",即为充满整个LinearLayout,假如屏幕依然为480dip,首先LinearLayout为3个TextView分配的宽度为480dip,屏幕剩余宽度为 480 - 3* 480 =-960dip,然后3个TextView按照权重分配剩余空间,第一个TextView分配宽度为 480 + (-960) *(1/5) = 288dip,后面两个TextView就为480 + (-960) * (2/5) =96dip,比例为3:1:1
通过上面的例子和分析,你是不是对Layout_weight属性理解很透彻了呢,如果我们想要按照权重比例来分配LinearLayout,我们需要将其宽度设置为0dip,如果我们将其宽度设置为“fill_parent"的时候,其控件所占的比例不是权重的比例,我们需要自行计算比例
接下来我们就通过Layout_weight用ListView来实现表格,我们先看Activity的布局
- <</SPAN>LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" -
xmlns:tools="http://schemas.android.com/tools" -
android:orientation="vertical" -
android:layout_margin="10dip" -
android:layout_width="match_parent" -
android:layout_height="match_parent" > -
-
<</SPAN>include -
layout="@layout/list_item" -
android:id="@+id/table_title"/> -
-
<</SPAN>ListView -
android:id="@+id/list" -
android:divider="#f9b68b" -
android:dividerHeight="1.0dip" -
android:scrollbars="none" -
android:background="@drawable/listview_bg" -
android:cacheColorHint="@android:color/transparent" -
android:fadingEdge="none" -
android:layout_width="fill_parent" -
android:layout_height="wrap_content" > -
</</SPAN>ListView> -
- </</SPAN>LinearLayout>
一个线性布局,然后就是表格的title布局,下面就是一个ListView了,很简单的布局,接下来就是ListView每个item的布局
- Android 对Layout_weight属性…
- 对android的layout_weight属性的理解
- 对android LinearLayout中layout_weight属性使用
- 对android的layout_weight属性的理解
- android---android:layout_weight属性
- android layout_weight 属性
- android:layout_weight属性详解
- Android layout_weight属性
- android:layout_weight属性详解
- android:layout_weight属性探讨
- android:layout_weight属性详解
- Android layout_weight属性备忘
- android:layout_weight属性详解
- android:layout_weight属性讲解
- Android layout_weight属性
- android:layout_weight属性详解
- android:layout_weight 属性
- android:layout_weight属性详解
- mysql语句-load data infile
- HTML处理收集(转)
- Vmware10下安装Mac OS X 10.8.5
- android中activity的生命周期
- Xcode6的新特性、iPhone6和iPhone6Plus的适配
- Android 对Layout_weight属性…
- 请放弃你无效的社交(转)
- github常用命令(转)
- SPOJ7758--- Growing Strings
- 逻辑备库运维相关的脚本
- 使用github上的开源框架SlidingMen…
- android控件的继承关系图
- javascript中获取一个对象的属性名…
- 舌尖上的中国第一季整理笔记