首页 文章

Android中的线性布局和重量

提问于
浏览
239

我总是在Android文档中读到这个有趣的重量值 . 现在我想第一次尝试它,但它根本不起作用 .

据我所知,这个布局的文件:

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

     <Button
        android:text="Register"
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        weight="1" />

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        weight="1" />

  </LinearLayout>

应创建两个水平对齐的按钮,并平等分享空间 . 问题是两个按钮不会增长以填充空间 .

我想按钮增长并填满整行 . 如果两个按钮都设置为仅匹配父按钮,则显示第一个按钮并填充整行 .

18 回答

  • 5

    另外,您需要为 LinerLayout 的儿童视图[按钮视图]添加此 android:layout_width="0dp"

  • 6
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/logonFormButtons"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="true"       
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/logonFormBTLogon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"            
                android:text="@string/logon"
                android:layout_weight="0.5" />
    
            <Button
                android:id="@+id/logonFormBTCancel"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"            
                android:text="@string/cancel"
                android:layout_weight="0.5" />
        </LinearLayout>
    
  • 2
    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="#008">
    
                <RelativeLayout
                    android:id="@+id/paneltamrin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
    
                    >
                    <Button
                        android:id="@+id/BtnT1"
                        android:layout_width="wrap_content"
                        android:layout_height="150dp"
                        android:drawableTop="@android:drawable/ic_menu_edit"
                        android:drawablePadding="6dp"
                        android:padding="15dp"
                        android:text="AndroidDhina"
                        android:textColor="#000"
                        android:textStyle="bold" />
                </RelativeLayout>
    
                <RelativeLayout
                    android:id="@+id/paneltamrin2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    >
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="150dp"
                         android:drawableTop="@android:drawable/ic_menu_edit"
                        android:drawablePadding="6dp"
                        android:padding="15dp"
                        android:text="AndroidDhina"
                        android:textColor="#000"
                        android:textStyle="bold" />
    
                </RelativeLayout>
            </LinearLayout>
    

    enter image description here

  • 1

    这是您问题的完美答案

    <LinearLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:orientation="horizontal"  >   
         <Button 
            android:text="Register" android:id="@+id/register"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:padding="10dip" weight="1" />
         <Button 
            android:text="Not this time" android:id="@+id/cancel"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:padding="10dip" weight="1" />
      </LinearLayout>
    
  • 0

    以下是代码中的更改(标记为 BOLD ):

    <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">
    
         <Button
            android:text="Register"
            android:id="@+id/register"
            android:layout_width="0dp" //changes made here
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_weight="1" /> //changes made here
    
         <Button
            android:text="Not this time"
            android:id="@+id/cancel"
            android:layout_width="0dp" //changes made here
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_weight="1" /> //changes made here
    
      </LinearLayout>
    

    由于LinearLayout的方向为水平方向,因此您需要保持 width only as 0dp. 以便在该方向上使用权重 . (如果你的方向是垂直的,你的高度只有0dp) .

    由于有两个视图,并且您为两个视图放置了 android:layout_weight="1" ,这意味着它将在水平方向(或宽度)上平均分割两个视图 .

  • 7

    尝试将两个按钮的 layout_width 设置为"0dip",将两个按钮的 weight 设置为 0.5

  • 4

    fill_parent 代替 wrap_content .

  • 2

    要记住的3件事:

    • 将孩子的 android:layout_width 设置为 "0dp"

    • 设置父级的 android:weightSum (编辑:正如Jason Moore注意到的,此属性是可选的,因为默认情况下它设置为子级的layout_weight总和)

    • 按比例设置每个孩子的 android:layout_weight (例如weightSum = "5",三个孩子:layout_weight = "1",layout_weight = "3",layout_weight = "1")

    例:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="5">
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="2" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />
    
        </LinearLayout>
    

    结果如下:

    Layout weight example

  • 21

    喜欢回答@Manoj Seelan

    android:weight 替换 android:layout_weight .

    WeightLinearLayout 一起使用时 . 您必须在 LinearLayout 中添加 weightSum 并根据 LinearLayout 的方向,您必须将 0dp 为宽度/高度设置为所有 LinearLayout 的子视图

    示例:

    If Linearlayout 的方向是 Vertical ,然后用 0dp 设置所有 LinearLayout 的子视图的宽度

    <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical"
         android:weightSum="3">
    
         <Button
            android:text="Register"
            android:id="@+id/register"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_weight="2" />
    
         <Button
            android:text="Not this time"
            android:id="@+id/cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_weight="1" />
    
      </LinearLayout>
    

    If 方向 Linearlayouthorizontal ,然后使用 0dp 设置所有 LinearLayout 的子视图的高度 .

    <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:weightSum="3">
    
         <Button
            android:text="Register"
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:padding="10dip"
            android:layout_weight="2" />
    
         <Button
            android:text="Not this time"
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:padding="10dip"
            android:layout_weight="1" />
    
      </LinearLayout>
    
  • 144

    你必须这样写它为我工作

    <LinearLayout
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal"
                android:weightSum="2">
    
             <Button
                android:text="Register"
                android:id="@+id/register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dip"
                android:layout_weight="1" />
    
             <Button
                android:text="Not this time"
                android:id="@+id/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dip"
                android:layout_weight="1" />
    
  • 15

    您没有设置 layout_weight 属性 . 您的代码为 weight="1" ,应为 android:layout_weight="1" .

  • 1

    在按钮的宽度字段中,将 wrap-content 替换为 0dp .
    使用视图的layout_weight属性 .

    android:layout_width="0dp"
    

    这就是你的代码的样子:

    <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
    
     <Button
        android:text="Register"
        android:id="@+id/register"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_weight="1" />
    
     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_weight="1" />    
    
    </LinearLayout>
    

    layout_weight用于将任何左侧空间分配为比例 . 在这种情况下,两个按钮的宽度为“0dp” . 因此,剩余空间将被分成1:1的比例,即空间将在按钮视图之间平均分配 .

  • 4

    这是 android:layout_weight . 重量只能在 LinearLayout 中使用 . 如果linearlayout的方向为Vertical,则使用 android:layout_height="0dp" ,如果方向为水平,则使用 android:layout_width = "0dp" . 它会很完美 .

  • 640

    也许将两个按钮layout_width属性设置为“fill_parent”就可以了 .

    我刚刚测试了这段代码,它可以在模拟器中运行:

    <LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content">
    
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="hello world"/>
    
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="goodbye world"/>
    
    </LinearLayout>
    

    务必在两个按钮上将layout_width设置为“fill_parent” .

  • 0

    在上面的XML中,将线性布局的 android:layout_weight 设置为 2android:layout_weight="2"

  • 0
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Button 1" />
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="Button 2" />
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Button 3" />
    
        </LinearLayout>
    
  • 51

    LinearLayout支持为各个孩子分配权重 . 此属性为视图指定“ importance ”值,并允许它展开以填充父视图中的任何剩余空间 . 默认权重为零

    计算以在子项之间分配任何 Remaining/Extra 空格 . (不是总空间)

    空间分配给孩子=(儿童个人体重)/(线性布局中每个孩子的体重总和)

    Example (1): 如果有三个文本框,其中两个声明权重为1,而第三个没有赋予权重(0),则 Remaining/Extra 空间分配给

    1st text box = 1/(1+1+0) 
    2nd text box = 1/(1+1+0) 
    3rd text box = 0/(1+1+0)
    

    Example (2) :假设我们在水平行中有一个文本标签和两个文本编辑元素 . 标签没有指定layout_weight,因此它占用了渲染所需的最小空间 . 如果两个文本编辑元素中每个文本编辑元素的layout_weight设置为1,则父布局中的剩余宽度将在它们之间平均分配(因为我们声称它们同样重要) .

    calculation : 
    1st label = 0/(0+1+1) 
    2nd text box = 1/(0+1+1) 
    3rd text box = 1/(0+1+1)
    

    如果第一个文本框的layout_weight为1,第二个文本框的layout_weight为2,则剩余空间的三分之一将被赋予第一个,而三分之二将被赋予第二个(因为我们声称第二个是更重要) .

    calculation : 
    1st label = 0/(0+1+2) 
    2nd text box = 1/(0+1+2) 
    3rd text box = 2/(0+1+2)
    
  • 0

    此图像总结了线性布局 .

    Linear Layout and Weight

    您可以点击此链接获取更多信息话题 . Just Maths - Views, View Groups and Layouts

    Video Tutorial For Linear Layout : Width, Height & Weights

    Android Linear Layout Tutorial

相关问题