0

我似乎无法弄清楚如何有效地使用 android 的 layout_weight 属性。我正在尝试创建一个具有三个 textView 的自定义 listView。主 textView 位于左上角,然后我有一个位于右上角的文本视图,然后是位于主 textView 下方的第三个文本视图。我希望主要的和右上角的两个在某个水平轴上,但我希望主要的 textView 占据大约 70% 的宽度,而另一个 textView 占据剩下的 30%。无论我为主要和右侧 textView 分配什么权重,主要 textView 总是更大,并且将正确的数据 textView 挤压得太薄。

在此处输入图像描述

我的代码:

<LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="5dip"
              android:id="@+id/Llayout">
<!-- Title-->
<TextView
        android:id="@+id/primaryTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading some tadglkj dgjg sadlgkj dgksgj sgj sdgk"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="18dip"
        android:textStyle="bold"
        android:paddingTop="0dp"
        android:background="#888"
        android:layout_weight="4"/>

<!-- Right Data -->
<TextView
        android:id="@+id/rightData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:layout_marginRight="5dip"
        android:textSize="12dip"
        android:textColor="#B53021"
        android:textStyle="bold"
        android:background="#bada55"
        android:text="1.3 mi"/>
</LinearLayout>

<!-- Secondary title -->
<TextView
        android:id="@+id/secondaryTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#343434"
        android:textSize="12dip"
        android:layout_marginTop="1dip"
        android:text="hello this is some other data"/>

4

4 回答 4

0

You need you need to define a weight sum in your container. In your case, this would look something like this (only including the relevant attributes you need):

<LinearLayout 
      android:weightSum="1">
<!-- Title-->
<TextView
     android:layout_weight=".7"/>

<!-- Right Data -->
<TextView
       android:layout_weight=".3"/>
</LinearLayout>
于 2013-05-14T14:42:30.557 回答
0

在你想成为 70%layout_weight="30"的项目上设置 ,在你想成为 30% 的项目上设置layout_weight="70"。那应该有望解决您的问题。

于 2013-05-14T14:39:22.483 回答
0

我需要做几件事。

  1. 将 weightSum 分配给包含 TextViews 的 LinearLayout
  2. 然后我需要将 TextViews 的 layout_width 更改为“0dp”而不是“wrap_content”

    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:id="@+id/Llayout"
                  android:weightSum="10">
    
        <!-- Title-->
        <TextView
                android:id="@+id/primaryTitle"
                android:layout_height="wrap_content"
                android:text="Loading..."
                android:textColor="#040404"
                android:typeface="sans"
                android:textSize="18dip"
                android:textStyle="bold"
                android:paddingTop="0dp"
                android:layout_width="0dp"
                android:layout_weight="8"/>
    
        <!-- Right Data -->
        <TextView
                android:id="@+id/rightData"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:gravity="right"
                android:layout_marginRight="5dip"
                android:textSize="12dip"
                android:textColor="#B53021"
                android:textStyle="bold"
                android:text="..."/>
    </LinearLayout>
    
    <!-- Secondary title -->
    <TextView
            android:id="@+id/secondaryTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#343434"
            android:textSize="12dip"
            android:layout_marginTop="1dip"
            android:text="..."/>
    

于 2013-05-14T14:56:01.720 回答
0

您只需将两个 TextViews 中的 android:layout_width="wrap_content" 更改为 android:layout_width="0dp"

android:layout_width="0dp"

不需要 android:weightSum。

于 2013-05-14T16:20:28.657 回答