首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >android:layout_weight是如何工作的?

android:layout_weight是如何工作的?
EN

Stack Overflow用户
提问于 2010-06-21 19:24:20
回答 6查看 62K关注 0票数 36

当我有以下内容时,它显示四种颜色的顶部布局区域比底部布局区域小得多。

根据this documentation的说法,当你向layout_weight添加更多内容时,它应该会增加面积,但在代码中它会减少。

代码语言:javascript
复制
<?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">

  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="4">
      <TextView
          android:text="red"
          android:gravity="center_horizontal"
          android:background="#aa0000"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="3"/>
      <TextView
          android:text="green"
          android:gravity="center_horizontal"
          android:background="#00aa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="blue"
          android:gravity="center_horizontal"
          android:background="#0000aa"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="yellow"
          android:gravity="center_horizontal"
          android:background="#aaaa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
  </LinearLayout>

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextView
        android:text="row one"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row two"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row three"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row four"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
  </LinearLayout>

</LinearLayout>
EN

回答 6

Stack Overflow用户

发布于 2011-01-19 09:39:46

我也有同样的问题。诀窍是不要对要设置权重的控件使用"wrap_content“或"fill_parent”。相反,将layout_height设置为0px (在垂直布局中时),然后子控件将根据权重值进行比例调整。

票数 41
EN

Stack Overflow用户

发布于 2010-06-21 20:05:19

如果您在LinearLayout中使用fill_parent,则布局将占用尽可能多的空间,并且稍后定义的所有布局项都必须处理剩余的空间。

如果您将两个LinearLayouts的高度都设置为wrap_content,则重量应按文档所示工作。

票数 10
EN

Stack Overflow用户

发布于 2012-04-09 08:34:23

我知道这为时已晚,但希望它能帮助人们:

在父级上使用android:weightSum。这里有一个到dev文档的链接。

http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum

在父视图上使用1.0android:weightSum,然后在带有android:layout_weight的子视图上使用0.x,以便它们使用该比例的空间。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3084185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档