layout_weight使用

layout_weight的真正含义是等比例分割剩余空间,

在线性布局(linearlayout)中对宽度和高度进行比例分割,

三个子linearlayout分割父linearlayout高度,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sunjianfei.linearlayout_test.MainActivity"
    android:orientation="vertical"
    android:id="@+id/linearall">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/linear1"
        android:background="#ff0000"
        android:gravity="center_horizontal">
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:id="@+id/linear2"
        android:background="#aa10ff"
        >

    </LinearLayout>

   <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:id="@+id/linear3"
        android:background="#000000"

        >
    </LinearLayout>
</LinearLayout>

三个linearlayout的比为1:2:3;

三个linearlayout的layout_height为"match_parent",那么剩余空间为1P-3P=-2P;

第一个linearlayout的高度:P+1/6*(-2P)=2/3P;

第二个linearlayout的高度:P+2/6*(-2P)=1/3P;

第三个linearlayout的高度:P+3/6*(-2P)=0;

所以三个linearlayout的实际高度比为2:1:0;

如果是两个linearlayout的比为1:2;

也是同样的计算,

剩余空间:P-2P=-P;

第一个linearlayout的高度:P+1/3*(-P)= 2/3P;

第二个linearlayout的高度:P+2/3*(-P)= 1/3P;

如果是四个五个平分也是同样的计算方法:

高度/宽度 = P+权*剩余空间

时间: 2024-08-01 16:13:11

layout_weight使用的相关文章

android:layout_weight

layout_weight : 用于指定剩余空闲空间的分割比例. 来源:http://blog.csdn.net/yanzi1225627/article/details/24667299  还有一句话很好理解 [定义了2个控件的 layout_width="0dp", layout_weight="1",实现了水平方向50%平均分割] 来源:www.cnblogs.com/needly/p/5876157.html

layout_weight及常见属性解析

我们看一下下面的代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:

ANDROID_MARS学习笔记_S01_007Linear_layout嵌套与layout_weight的设置

一.介绍 二.1.linear_layout.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height=&

Android UI: LinearLayout中layout_weight 属性的使用规则

首先来查看android sdk文档,有这么一段话 LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space is should occupy on the sc

LinearLayout中的layout_weight属性详解

线性布局中的layout_weight顾名思义即为定义各个组件在整个界面中所占的权重.常用两种实现方法 1:最为简单的实现方法,当orientation为水平时垂直layout_height=''0dp",同理当orientation为垂直时,将layout_width设定为0dp,此时在一个直接父布局中layout_width中的数值越大这个组件占的界面就越大.示例代码如下: <LinearLayout xmlns:android="http://schemas.android

weightSum和layout_weight属性合用

讲解一:weightSum和layout_weight属性合用 android:weightSum属性:定义weight总和的最大值.如果为指定该值,所有子视图的layout_weight属性的累加值作为总和的最大值.例如,通过指定子视图的layout_weight属性为0.5,并设置LinearLayout的weight属性为1.0,实现子视图占据可用宽度的50%: layout_weight属性:子视图的控件比例.就像我们在盒子放置其他物体,盒子可用空间的比例就是weightSum,盒子每个

android 布局中 layout_gravity、gravity、orientation、layout_weight

线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是 android:layout_gravity ( 是本元素相对于父元素的重力方向 ) android:gravity (是本元素所有子元素的重力方向) android:orientation (线性布局以列或行来显示内部子元素) android:layout_weight (线性布局内子元素对未占用空间[水平或垂直]分配权重值,其值越小,权重越大. 前提是子元素 设置了 android:layout_width = "

layout_weight 全解析

[layout_weight 全解析] 参考:http://www.cnblogs.com/net168/p/4227144.html

android:layout_weight属性的使用方法总结

原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6282826.html android:layout_weight属性可以和其他属性配合使用,产生多种效果,但如果我们不清楚各种配合的使用,也容易产生一些 意想不到的结果,今天我们就认真的总结一下android:layout_weight属性的各种用法和产生的效果,方便今后直接拿来使用. 首先声明一句,我们一般只在LinearLayout中使用该属性,以下各种情况都是在LinearLayout中产

Layout_weight

首先按照控件的的尺寸进行分配,然后剩下的空间在按照weight的比例进行分配 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="matc