关于layout_weight

一直对layout_weight似懂非懂,今天好好去网上搜了些资料,下面是一个比较好的解释:

layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。

所有的视图都有一个layout_weight值,默认为零,意思是需要显示 多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视 图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight  值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。

举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。 该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。 如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要度越高)。

来几个例子:

布局管理器是这样的:

<?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"
    android:layout_weight="4"
    >
	<TextView  
	android:id="@+id/text1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:layout_weight="0"
    />
    
    <TextView  
    android:id="@+id/text2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:layout_weight="1"
    />
    
    <TextView  
    android:id="@+id/text3"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:layout_weight="2"
    />
</LinearLayout>

主程序如下,为了方便观察,我给textview加上了不同的颜色:

<?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"
    android:layout_weight="4"
    >
	<TextView  
	android:id="@+id/text1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:layout_weight="0"
    />
    
    <TextView  
    android:id="@+id/text2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:layout_weight="1"
    />
    
    <TextView  
    android:id="@+id/text3"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:layout_weight="2"
    />
</LinearLayout>


结果如下:

 

因此可以看出,比例是1:2:3,并不像网上大多数人所说,weight值越小,重要度越高。

然后我将LinearLayout的方向设置为水平,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TextView  
	android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:layout_weight="2"
    />
    
    <TextView  
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:layout_weight="1"
    />
    
    <TextView  
    android:id="@+id/text3"
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:layout_weight="1"
    />
</LinearLayout>


此时运行结果却是:

而把TextView的layout_weight分别设为2,1,1时,此时结果是:

这样的结果,就是权值越小,重要性越大了。上面的结论就成立了。

将layout_wight分别设为2,1,2结果为:

进一步证明了以上结论。


但是奇怪的事还没完。当我把TextView的内容改短了一些,改为"text"并把layout_width设为wrap_content,main.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TextView  
	android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="text"
    android:layout_weight="2"
    />
    
    <TextView  
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="text"
    android:layout_weight="1"
    />
    
    <TextView  
    android:id="@+id/text3"
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content" 
    android:text="text"
    android:layout_weight="2"
    />
</LinearLayout>


此时的运行结果居然是:

此时又与上面的结论:weight越小,重要性越大相违背了。。。。
 
奇怪的是如果我把layout_width设为fill_parent的话,结果如下:
又符合了上面的结论。
 
面对这几种不同的情况,我彻底的晕了。。。这水平情况和垂直情况不相同,fill_parent和wrap_context也不相同。网上很多资料都是片面性的。我想可能对于layout_weight我还没真正搞清楚,先把这个问题记下来,日后若解决了,再把结果贴出来。
 
如果需要运用到weight,其实也没有什么问题,多试几次就可以了。

 

你可能感兴趣的:(android,layout,文本编辑,encoding)