工作当中使用layout_weight的一些细节问题

按照惯例我要声明文章不得转载!

 

按照惯例我要解释一下为什么时隔一年才又一次在cnblogs 上写博客。话说从上次写之后我工作就比较忙,这里我并没有想为自己开脱,事实上确实忙(因为刚到新公司接手新项目),但是这么长时间以来难道我真的就没有时间写blog?不是的事实上我一直想努力坚持写,但是人总是邪恶的!(没有dota的人生是不完整的)。当然出了这些之外我还进入了“围城”,以后漂亮的mm 对我来说也只能看看作罢!进入围城之后我回到了自己的家乡在新公司做新东西,结果又是一阵忙!

 

那么为什么现在又开始写了呢?

经过了新公司的项目我感觉有些知识点还是要好好积累一下的比如标题的只是点,大家通常都会用到,但是谁会好好的看看比较一下呢?好吧,你一定嫌我罗嗦了,直接说正题吧。

 

layout_weight 的使用范围?

layout_weight目前只在LinearLayout这个布局中出现(其他的布局截止到目前写博位置还没有出现如果你有新发现告诉我)

 

layout_weight使用只能对水平有用么?

答案是否定的,在垂直方面也是可以的,待会你将会从我的例子中看到(事实胜于雄辩)

 

在使用layout_weight的时候layout_width layout_height 有什么需要注意的事项么?

答案是肯定的,这一点也将会在我的例子中出现

好了目前先说这么多,如果你有新发现请给我留言

看图

 

解释一下:这个布局把整个screen 分成了三块, A区 我们指左上角 内容是helloworld linearlayouactivity B 区我们指 右上角 内容是123456789  C区我们指 最下方的一块区域 内容是hello1234

 

接下来解释一下布局: 首先整个界面第一层是LinearLayout 然后我们在里面分了一个LinearLayout,这个LinearLayout用来布局上方的 AB 区,下方的C区我使用TextView 代替的。

看一下代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:singleLine="true"
            android:text="@string/hello" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:text="123456789" />
    </LinearLayout>
     <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:text="hello1234" />
    

</LinearLayout>

你没有看错,是的我们整个布局使用LinearLayout然后我们在     垂直    方向使用layout_weight,我们让 C区的TextView 占了4 个比重。,你可以看到他的空间的确是很大,至少比上面的LinearLayout区域要大。这是至少证明我第一点说的是对的,难道不是么?

 

接着我就来解释我的第三点结论, 我们从上图可以看到目前A区比B区 占的空间要大,而且我们要注意到 这一点代码

 <LinearLayout 
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="2"
        android:orientation="horizontal">
        <TextView 
            android:layout_height="wrap_content"
                android:layout_width="wrap_content"
            android:layout_weight="2"
            android:singleLine="true"
            android:text="@string/hello" />
        <TextView
            android:layout_height="wrap_content"
                android:layout_width="wrap_content"
            android:layout_weight="4"
            android:text="123456789" />
    </LinearLayout>
            

注意这里的LinearLayout 里面TextView 的layout_width  我定义的是"wrap_content"。现在如果我把 layout_width 设置 “0dp”, 你们觉得效果会是什么样子?

好吧截图就知道了,

你会发现这里的布局竟然发生了转变?这样我们是不是可以得出一个结论?(当然这里只是推测)

 

前提:给组件的Layout_width 指定具体的数值

设置layout_weight 的时候是 首先根据layout_width 来判断的,如果layout_width的值(假如是120dp) 比 定义的layout_weight 指定的比重(加入知道那个比重是2,而整个界面的比重是6 这个组件设置了2,也就是占了三分之一,那么这个是这的宽度应该是160px 换算成dp的话我的 密度是1.5 也就是106dp左右 ),界面所分配的宽, 大的话我按照layout_width 的值作为组件的宽,否则 就按照layout_weight的比重分配的空间做为组件的宽!

 

如果没有给Layout_width 指定具体的数值而是 指定 wrap_content 会怎么样呢?

那当然是谁设置的layout_weight 值小 谁占用的空间越大!

 

备注:如果看blog 的你有什么新的发现请给我留言,希望大家共同学习!

 

 

 

 

 

 

 

 

posted @ 2013-04-09 22:04  android007  阅读(1210)  评论(1编辑  收藏  举报