android 系统下的weight属性学习总结


作为一名专业的android初学者,哥一头撞在weight属性上,头破血流。
1. 开始使用weight属性的时候,仅仅是对于左右两个TextView元素的时候,那时候对于weight的理解,认为是比例。例如:1:2,表示左边占据2/3,右边占据1/3(文档上说数字越大,所占空间越小)
2. 后来接触到3个元素的情况,要求中间的部分居中显示,上下部分贴上边和下边。也就是说相当于页眉和页脚。开始的时候是使用fill_parent实现,上下部分wrap_content,中间的fill_parent,但是由于上下部分的高度有可能变化,于是这时候,经常是页眉没问题,但是页脚被撑下去,撑出屏幕外边。百般无奈,最后找到解决方案,高度,全都是wrap_content,中间部分只有中间部分增加weight属性,只要不是0,就会自动计算先可着上下两部分显示,然后它占据所有剩下的空间。
3. 最后接触到一排元素并列放到一起,但是宽度或者高度需要按比例放置。怎么实现?在父容器中设置weightSum,然后给各个控件设置weight,并且设置宽度(或者高度)为"0px",此时,是按照weight的比例大小分配的,注意!!!!这里是weight越大,占据的空间越大。

我晕!!!!!!不知道你们晕了没有。

所以,我就做个实验试试看。
你猜怎么着,灰常灰常复杂!
首先决定因素有很多: 
1. 单独使用xml进行配置
2. 使用代码写LayoutParams.weight
3. 设置本控件的width为0px,fill_parent,wrap_content
4. 本控件的内部内容需求(由内容撑开)
5. Container(例如LinearLayout)和Content内容控件(例如:TextView)

本人实验出的规则有以下几点:
1. 当对象是Container的时候,如果设置了width为fill_parent,是无论如何不会显示第三个控件的。也就是说,FillParent的Container只能有两个,在一行或者一列。即使第三个也是fill_parent也不行。而此时,weight比例是反比的,也就是1:2的时候,实际宽度为 2:1
2. 当Container的width为wrapcontent的时候,如果任何一个中间有控件,则仅会显示有控件的那个。其余两个不见了。。。
3. 当Container的width设置为0px的时候,此时分配的weight是成正比的。1:2的时候,实际宽度就是1:2
4. 当Container的时候代码设置weight和xml配置效果一样。
5. 当对象是Content的时候,(TextView),xml的配置和代码指定weight的效果是不一样的。
6. 当是Content的时候,如果内容很少,且是使用xml配置weight的时候,是正比例显示的。如果内容很多,最大限度的时候,又是反比例显示的。但是,如果配置了宽度为0px,不管内容是多少,都是正比例显示的。而当使用代码配置weight的时候,无论内容多少,无论宽度是什么,一定是按照反比例显示的。
7. 如果宽度不是0px,则实际的效果会千差万别,大体按照weight值的正反比例安排空间,但是具体的值。。。哥没有找到规律。很简单的一个宽度比例:1:2:3,内容都是撑开的,也就是内容都很大,width设置为wrap_content,fill_parent效果一样。结果得到的宽度比例是:226px  + 195px + 59px (屏宽480px这倒没错)。谁能告诉我为什么!!!!!

本来是想整几副图的。。。可是,太多了。整不过来了。代码放在最后,自己调整,看看效果吧。

总结:
A。当使用weight属性的时候,最好指定宽度为0px,因为此时是严格按照比例关系来配置的,不管是正比例还是反比例。
B。记得给content内容使用代码设置weight属性的时候,比例值是反比的。尽量使用同一种方法配置Content内容的比例值。我还没有试过并列两个控件的weight一个使用xml配置,一个使用代码写是什么效果。。。神啊。你自己试吧!
C。记得测试测试测试!很可能你的内容不一样的时候,比例关系也乱了。。。哈哈,遵守   A   吧,让你免除这个烦恼。

代码:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="200px"
android:background="#55FF0000"
android:orientation="horizontal" 
>
<LinearLayout android:layout_width="0px"
android:layout_height="fill_parent"
android:background="#55CCBBCC"
android:layout_weight="1.0"
>
</LinearLayout>
<LinearLayout android:layout_width="0px"
android:layout_height="fill_parent"
android:background="#5500BBCC"
android:layout_weight="2.0"
>
</LinearLayout>
<LinearLayout android:layout_width="0px"
android:layout_height="fill_parent"
android:background="#55CC00CC"
android:layout_weight="3.0"
android:orientation="horizontal"
>
<TextView android:layout_weight="1.0"
android:layout_width="wrap_content" 
android:layout_height="fill_parent"
android:text="111111111111111111111111111111111111111111111111111111"
android:background="#55987240"
/>
<TextView android:layout_weight="2.0"
android:layout_width="wrap_content" 
android:layout_height="fill_parent"
android:text="21222222222222222222222222222222222222222222222222"
android:background="#55456123"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="200px"
android:background="#5500FF00"
android:orientation="horizontal" 
>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#55CCBBCC"
android:layout_weight="1.0"
android:id="@+id/lt1"
>
<TextView android:layout_weight="1.0"
android:layout_width="wrap_content" 
android:layout_height="fill_parent"
android:text="11111111111111111111111111111111111111111"
android:background="#55987240"
/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#55CC00CC"
android:layout_weight="2.0"
android:id="@+id/lt2"
>
<TextView android:layout_weight="2.0"
android:layout_width="wrap_content" 
android:layout_height="fill_parent"
android:text="22222222222222222222222222222222222222222222222222"
android:background="#55456123"
/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#55CCBB00"
android:layout_weight="3.0"
android:weightSum="6.0"
android:orientation="horizontal"
android:id="@+id/lt3"
>
<TextView android:layout_weight="3.0"
android:layout_width="wrap_content" 
android:layout_height="fill_parent"
android:text="3333333333333333333333333333333333333333333333"
android:background="#55123456"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="200px"
android:background="#550000FF"
android:orientation="horizontal"  
android:weightSum="6.0"
>
<LinearLayout android:layout_width="0px"
android:layout_height="fill_parent"
android:background="#55CCBBCC"
android:layout_weight="1.0"
>
</LinearLayout>
<LinearLayout android:layout_width="0px"
android:layout_height="fill_parent"
android:background="#55CC00CC"
android:layout_weight="2.0"
>
</LinearLayout>
<LinearLayout android:layout_width="0px"
android:layout_height="fill_parent"
android:background="#55CCBB00"
android:layout_weight="3.0"
android:orientation="horizontal"
android:weightSum="6.0"
>
<TextView android:layout_weight="1.0"
android:layout_width="0px" 
android:layout_height="fill_parent"
android:text="1111111111111111111111111111111"
android:background="#55987240"
android:id="@+id/txt1"
/>
<TextView android:layout_weight="2.0"
android:layout_width="0px" 
android:layout_height="fill_parent"
android:text="222222222222222222222222222222"
android:background="#55456123"
android:id="@+id/txt2"
/>
<TextView android:layout_weight="3.0"
android:layout_width="0px" 
android:layout_height="fill_parent"
android:text="333333333333333333333333333333"
android:background="#55123456"
android:id="@+id/txt3"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

//        LinearLayout lt1 = (LinearLayout)findViewById(R.id.lt1);
//        ((LinearLayout.LayoutParams)lt1.getLayoutParams()).weight = 1.0f;
//        LinearLayout lt2 = (LinearLayout)findViewById(R.id.lt2);
//        ((LinearLayout.LayoutParams)lt2.getLayoutParams()).weight = 2.0f;
//        LinearLayout lt3 = (LinearLayout)findViewById(R.id.lt3);
//        ((LinearLayout.LayoutParams)lt3.getLayoutParams()).weight = 3.0f;
        
//        TextView t1 = (TextView)findViewById(R.id.txt1);
//        ((LinearLayout.LayoutParams)t1.getLayoutParams()).weight = 1.0f;
//        TextView t2 = (TextView)findViewById(R.id.txt1);
//        ((LinearLayout.LayoutParams)t2.getLayoutParams()).weight = 2.0f;
//        TextView t3 = (TextView)findViewById(R.id.txt1);
//        ((LinearLayout.LayoutParams)t3.getLayoutParams()).weight = 3.0f;
        
        
    }

android 系统下的weight属性学习总结 - 火星人 - 火星人的博客
 
android 系统下的weight属性学习总结 - 火星人 - 火星人的博客
 
android 系统下的weight属性学习总结 - 火星人 - 火星人的博客
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值