首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >了解重量和layout_weight,layout_weight越大,布局越缩小。

问了解重量和layout_weight,layout_weight越大,布局越缩小。
EN

Stack Overflow用户
提问于 2015-05-30 04:22:14
回答 2查看 2.2K关注 0票数 3

我试着用这个例子来理解权重布局。

这绝对不是一门火箭科学。然而,这个例子.

  • weightSum决定了它有多大
  • 然后根据layout_weight值对LinearLayout视图进行布局划分。

在本例中,我有一个布局,权重为5,然后将其划分为两个视图:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/transactionRowBackground"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:layout_weight="2" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="Test Title"
            android:textColor="@color/textColor"
            android:textSize="@dimen/subHeadingTextSize"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/description"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="This is a test description"
            android:textColor="@color/textColor"
            android:textSize="@dimen/normalTextSize" />

    </LinearLayout>


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="top"
        android:contentDescription="" />

</LinearLayout>

我无法理解的是,我给ImageViewer的数字越大,它从父母那里得到的空间就越小。那么它实际上是如何计算ImageView的大小的。

您可以尝试使用上面的xml。如果您将ImageView的布局权重更改为1,将子线性布局更改为4,我认为这更有意义,则会出现相反的情况。

ImageView将进行扩展,子线性布局将缩小。我以为这个数字越大你就能得到更多的空间。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-30 04:54:59

由于在最外层的布局上有android:orientation="horizontal",我相信您希望改变ImageView和内部LinearLayout在水平方向上所占的大小/空间。对于这种情况,请尝试使用

代码语言:javascript
复制
android:layout_width="0dp"

在您放置android:layout_weight的布局上。如果您的外部布局方向是垂直的,我将使用android:layout_height="0dp"来处理布局的宽度/高度。

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/transactionRowBackground"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:layout_weight="2" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="Test Title"
            android:textColor="@color/textColor"
            android:textSize="@dimen/subHeadingTextSize"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/description"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="This is a test description"
            android:textColor="@color/textColor"
            android:textSize="@dimen/normalTextSize" />

    </LinearLayout>


    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="top"
        android:contentDescription="" />

</LinearLayout>

阅读Android文档可能会有所帮助:布局权重

票数 3
EN

Stack Overflow用户

发布于 2015-05-30 04:56:52

使用layout_weight,您可以指定多个视图之间的大小比。例如,您有一个MapView和一个表,它应该显示地图上的一些附加信息。地图应该使用屏幕的3/4,表应该使用屏幕的1/4。然后将映射的layout_weight设置为3,将表的layout_weight设置为1。

要使其工作,您还必须将高度或宽度(取决于您的方向)设置为0dp。示例

如果有三个文本框,其中两个文本框声明权重为1,而第三个文本框没有权重(0),则剩余空间分配如下:

第一个文本框= 1/(1+1+0) 第二个文本框= 1/(1+1+0) 第三个文本框= 0/(1+1+0)

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

https://stackoverflow.com/questions/30542221

复制
相关文章

相似问题

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