`
darrenzhu
  • 浏览: 797326 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android:Layout_weight的深刻理解

阅读更多

原文链接:http://mobile.51cto.com/abased-375428.htm

首先看一下LinearLayout布局中Layout_weight属性的作用:它是用来分配剩余空间的一个属性,你可以设置他的权重。很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间。

看下面代码:

<?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"     
    >     
<EditText     
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
    android:gravity="left"     
    android:text="one"/>     
<EditText     
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
    android:gravity="center"     
    android:layout_weight="1.0"     
    android:text="two"/>     
    <EditText     
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
    android:gravity="right"     
    android:text="three"/>     
</LinearLayout>     

 

运行结果是:

看上面代码:只有Button2使用了Layout_weight属性,并赋值为了1,而Button1和Button3没有设置Layout_weight这个属性,根据API,可知,他们默认是0

下面我就来讲,Layout_weight这个属性的真正的意思:Android系统先按照你设置的3个Button高度Layout_height值wrap_content,给你分配好他们3个的高度,

然后会把剩下来的屏幕空间全部赋给Button2,因为只有他的权重值是1,这也是为什么Button2占了那么大的一块空间。

有了以上的理解我们就可以对网上关于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="wrap_content"   
    android:orientation="horizontal" >   
    <TextView   
        android:background="#ff0000"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="1"   
        android:textColor="@android:color/white"   
        android:layout_weight="1"/>   
    <TextView   
        android:background="#cccccc"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="2"   
        android:textColor="@android:color/black"   
        android:layout_weight="2" />   
     <TextView   
        android:background="#ddaacc"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="3"   
        android:textColor="@android:color/black"   
        android:layout_weight="3" />   
</LinearLayout> 

 

三个文本框的都是 layout_width=“wrap_content 时,会得到以下效果

按照上面的理解,系统先给3个TextView分配他们的宽度值wrap_content(宽度足以包含他们的内容1,2,3即可),然后会把剩下来的屏幕空间按照1:2:3的比列分配给3个textview,所以就出现了上面的图像。

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

你会发现1的权重小,反而分的多了,这是为什么呢???网上很多人说是当layout_width=fill_parent时,weighth值越小权重越大,优先级越高,就好像在背口诀

一样,其实他们并没有真正理解这个问题,真正的原因是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的话,会出现下面的效果了:

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

系统先给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的比列显示了。第三个就直接没有空间了。

分享到:
评论

相关推荐

    2011.10.13(4)——— android android:layout_weight

    在Android开发中,`android:layout_weight`是一个非常重要的属性,尤其在布局管理器中,如LinearLayout。这个属性主要用于在有限的空间内分配组件的大小,根据权重比例来决定每个子视图占据的屏幕空间。本篇文章将...

    Android 五种Layout 布局

    在Android开发中,布局(Layout)是构建用户界面的基础元素,它定义了屏幕上各个组件的排列方式和相互关系。本文将深入探讨Android的五种主要布局:LinearLayout、RelativeLayout、FrameLayout、GridLayout以及...

    android 各种Layout用到的一些重要的属性

    ### Android各种Layout的重要属性 #### 一、第一类:布尔属性值 这一类属性值主要用来控制视图在容器中的位置关系,常见的属性包括: - **`android:layout_centerHorizontal`**:设置此属性为 `true` 可以使视图...

    android:layout_gravity和android:gravity的区别

    1.首先来看看Android:layout_gravity和android:gravity的使用区别。 android:gravity: 这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置。例如,在一个Button按钮控件中设置如下两个属性, android...

    androidlayout-marginBottom的值为负数.docx

    首先,让我们理解`android:layout_marginBottom`的基本作用。当它的值为正数时,它会使View在垂直方向上向下移动一定的像素量,从而与下方的元素保持更多的间距。例如,如果将一个View的`android:layout_...

    Android中的android:layout_weight使用详解

    在Android开发中,`android:layout_weight`是一个关键属性,特别是在使用`LinearLayout`进行界面布局时。`layout_weight`用于指定一个...正确理解和使用`layout_weight`能够大大提高你在Android开发中的布局设计能力。

    Android_layout.rar_android_android 布局_layout

    关键属性有`orientation`(设置布局方向,可选垂直或水平)、`weight`(分配子视图的额外空间比例)以及`layout_gravity`(设置子视图在父视图中的位置)。 2. **RelativeLayout**:相对布局允许子视图相对于其他...

    Android中gravity与layout_gravity的使用区别分析

    android:layout_gravity:设置控件本身相对于父控件的显示位置。 看下如下代码段 代码如下:&lt;?xml version=”1.0″ encoding=”utf-8″?&gt;&lt;!– android:gravity设置了按钮上面的文字的显示位置,而android:...

    ex07_layout.rar_android_layout布局

    每个子组件可以设置权重`android:layout_weight`,以按比例分配空间。 4. **相对布局(RelativeLayout)**: 相对布局允许组件相对于其他组件或父布局的边缘定位。每个组件的位置可以通过`android:layout_toLeftOf...

    Hello_layout_demo

    在`Hello_Layout`这个示例项目中,开发者可以深入理解这些布局的工作原理,通过实际操作来调整不同参数,观察布局的变化,从而提升对Android布局设计的理解和应用能力。实践中,根据应用场景选择合适的布局类型,...

    Android Layout样式布局

    - 示例:`android:layout_weight="1"` - **android:padding** - 用途:设置控件内部内容与边界的距离。 - 示例:`android:padding="10dp"` - **android:singleLine** - 用途:若设置为true,则强制控件内容在...

    Android的Layout完全介绍

    android:layout_weight="1" android:layout_height="wrap_content" android:text="Button 1" /&gt; android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:...

    android_QQ_例子

    用Eclipse加载项目工程 &lt;LinearLayout xmlns:android=... android:layout_weight="0.66" android:background="@drawable/blue_bg" android:orientation="vertical" &gt; android:layout

    android layout

    ### Android Layout 概述 在Android开发中,`Layout`起着至关重要的作用,它用于组织和排列用户界面中的各种视图(View)组件。通过使用不同的布局方式,开发者可以创建出灵活且适应不同屏幕尺寸的应用界面。本文将...

    class_layout_demo.rar_DEMO

    你可以通过`android:orientation`属性设置方向,`android:layout_weight`属性分配子视图的权重,以实现按比例分配空间。 2. **相对布局(RelativeLayout)**:允许视图相对于其他视图的位置进行定位,提供了更灵活...

    ANDROID:控件属性(很全).

    * ID 引用属性:android:layout_below、android:layout_above、android:layout_toLeftOf、android:layout_toRightOf、android:layout_alignTop、android:layout_alignLeft、android:layout_alignBottom、android:...

    Android 方形layout界面

    android:layout_weight="1" android:layout_height="0dp" android:adjustViewBounds="true" android:scaleType="centerCrop" /&gt; ``` 3. **填充数据** 在代码中,我们需要动态地向`TableLayout`中添加`Table...

    android-autofittextview-master.zip_android_android textview_auto

    android:layout_width= match_parent android:layout_height= wrap_content &gt; &lt;Button android:layout_width= match_parent android:layout_height= wrap_content android:singleLine= true /&gt...

    Android 控件说明

    - `layout_weight`:用于确定视图在容器中的相对大小,特别是在`LinearLayout`中,当设置了`layout_weight`时,如果`layout_width`或`layout_height`设置为`match_parent`,则会根据权重分配空间。 - `layout_...

Global site tag (gtag.js) - Google Analytics