安卓开发技巧1:深入理解Android布局中Layout_weight的属性

安卓开发技巧一:深入理解Android布局中Layout_weight的属性

今天开始将要为大家介绍一些安卓开发过程将要用到的一些技巧,这些技巧全部来自网络搜集,或者自己在企业做项目的时候总结出来的,利用这些技巧将会对我们开发带来非常方便的便捷性。

先来记录一下这一段时间的技巧目录,方便大家以后方便查阅(大概有不到三十种的技巧总结,大概每周分享两个技巧,笔者将尽可能写的详细,以及提供实例源码)


安卓开发技巧一:深入理解Android布局中Layout_weight的属性

      安卓开发技巧二:自定义日志工具类

      安卓开发技巧三:Activity的启动模式

      安卓开发技巧四:分享一个工具包,方便快捷切换安卓UI线程及其他后台线程的一个工具包


    首先来介绍Android布局中Layout_weight的属性,它是用来分配剩余空间的一个属性,通过该属性可以设置布局的权重。说到剩余空间,想必很多人都不是特别的明白,下面我就先介绍一下剩余空间究竟为何方神圣:

    首先来看一段代码:

<?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" >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:background="#22ffff"
        android:text="one" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:gravity="center"
        android:background="#ff22ff"
        android:text="two" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:background="#ffff22"
        android:text="three" />

</LinearLayout>
运行结果如下图:

安卓开发技巧1:深入理解Android布局中Layout_weight的属性

我们来分析一下上面的代码:只有Button2使用了Layout_weight属性,并赋值为了1,而Button1和Button3没有设置Layout_weight这个属性,根据API,可知,他们默认则是赋值为0。下面我就来讲,Layout_weight这个属性的真正的意思:Android系统先按照你设置的3个Button高度Layout_height值wrap_content,给你分配好他们3个的高度,然后会把剩下来的屏幕空间全部赋给Button2,因为只有他的权重值是1,这也是为什么Button2占了那么大的一块空间。

接下来分析这段代码:

<?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="wrap_content"   
    android:orientation="horizontal" >   
    <TextView   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:text="1"   
        android:layout_weight="1"
        android:background="#00ffff"/>   
        <TextView   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:text="2"   
        android:layout_weight="2"
        android:background="#ff00ff"/>   
        <TextView   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:text="3"   
        android:layout_weight="3"
        android:background="#ffff00"/>   
    
</LinearLayout> 

效果图如下:

安卓开发技巧1:深入理解Android布局中Layout_weight的属性

下面我们来重点分析一下这一块:在上述代码里面,我们将三个文本框的 layout_width全部设置成了wrap_content ,这就说明这三块内容分别占了他们自己内容的大小再加上(将剩余空间分成1:2:3的比例)的部分,就是上图显示的空间大小。

而当layout_width=“fill_parent时,如果分别给三个TextView设置他们的Layout_weight122的话,就会出现下面的效果:

安卓开发技巧1:深入理解Android布局中Layout_weight的属性

这时候我们会发现,权重越小,反而分配的空间越多,这时产生的原因就是layout_width=“fill_parent导致的,具体原因如下

依照上面理解我们来分析:系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )那么第一个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/5 * 剩余空间大小(-2 parent_width)=3/5parent_width同理第二个TextView的实际所占宽度=parent_width + 2/5*(-2parent_width)=1/5parent_width;第三个TextView的实际所占宽度=parent_width + 2/5*(-2parent_width)=1/5parent_width;所以就是3:1:1的比列显示了。

这样你也就会明白为什么当你把三个Layout_weight设置为1、2、3的话,会出现下面的效果了:

安卓开发技巧1:深入理解Android布局中Layout_weight的属性

第三个直接不显示了,为什么呢?一起来按上面方法算一下吧:

系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )那么第一个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/6 * 剩余空间大小(-2 parent_width)=2/3parent_width同理第二个TextView的实际所占宽度=parent_width + 2/6*(-2parent_width)=1/3parent_width;第三个TextView的实际所占宽度=parent_width + 3/6*(-2parent_width)=0parent_width;所以就是2:1:0的比列显示了。第三个就直接没有空间了。

这个就是一些布局设置,没必要提供源码了吧,下面的一些技巧若是需要,我将会免费免积分的分享给大家,谢谢!