【问题标题】:Understanding weight and layout_weight, the bigger layout_weight is, more it shrinks in the layout.了解weight和layout_weight,layout_weight越大,在布局中收缩的越多。
【发布时间】:2015-05-30 12:56:52
【问题描述】:

我试图通过这个例子来理解重量布局。

这绝对不是一门火箭科学。但是,这个例子使它...

  • weightSum 决定了大小和
  • 然后在LinearLayout中根据view的layout_weight值划分布局。

在这个例子中,我有一个布局,权重为 5,然后在两个视图之间划分:

<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 将扩展,子线性布局将缩小。我认为数字越大,您获得的空间就越大。

【问题讨论】:

    标签: android xml android-layout layout


    【解决方案1】:

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

    android:layout_width="0dp"
    

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

    <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 文档可能会有所帮助:Layout Weights

    【讨论】:

    • 谢谢 我忘记了规则,一旦你有一个重量,你必须将宽度设置为 0dp。否则,权重被忽略。
    【解决方案2】:

    使用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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多