【问题标题】:Weight doesn't limit width of layout重量不限制布局的宽度
【发布时间】:2016-10-28 04:09:00
【问题描述】:

我有一个我想展示的项目列表。我写了以下代码
对于每个项目的视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingEnd="12dp"
    android:paddingStart="8dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp">
    <LinearLayout
        android:orientation="horizontal"    
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="6"
        android:gravity="center" >
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="4">
            <TextView
                android:id="+@id/receipt_item_name"
                android:layout_width="wrap_content"
                android:textAlignment="viewStart"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:text="item name" />
            <TextView
                android:layout_width="wrap_content"
                android:textAlignment="viewStart"
                android:layout_height="wrap_content"
                android:layout_below="@id/receipt_item_name"
                android:text="selected by 2 others"
                android:textSize="10sp"/>
        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAlignment="textEnd"
            android:text="10$" />
    </LinearLayout>
</RelativeLayout>

但是当receipt_item_name中的文本太长时,它会成为下一个
textview 右移。如何确保每个视图的权重可靠?
或者我还能做些什么来获得同样的效果?

【问题讨论】:

    标签: android xml android-layout android-studio android-recyclerview


    【解决方案1】:

    将具有weight 的每个布局的width 更改为0,如下所示:

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

    对于这两个也是如此:

    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAlignment="textEnd"
            android:text="10$" />
    

    问题是,当您同时设置weightwidth 时,width 优先并忽略weight 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      相关资源
      最近更新 更多