【问题标题】:Element with wrap_content width takes full width of parent具有 wrap_content 宽度的元素占据父级的全宽
【发布时间】:2015-09-30 03:16:40
【问题描述】:

我有以下布局 XML。不知道为什么第三个TextView 在布局中根本不可见。谁能指出原因?

布局 XML 的相关部分 -

<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toLeftOf="@+id/layout0"
    android:layout_toStartOf="@+id/layout0"
    android:layout_toRightOf="@+id/imageMain1"
    android:layout_toEndOf="@+id/imageMain1"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:paddingBottom="6dp"
    android:paddingTop="6dp">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:paddingBottom="2dp"
        android:text="@string/dummy_long"
        android:textColor="?android:textColorPrimary"
        android:textSize="16sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:layout_marginEnd="8dp"
            android:maxLines="1"
            android:text="@string/dummy_paragraph"
            android:textColor="?android:attr/textColorTertiary"
            android:textSize="14sp"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="@string/dummy"
            android:textColor="?android:attr/textColorTertiary"
            android:textSize="14sp"/>

    </LinearLayout>

</LinearLayout>

更新

最终使用了这个解决方案 - 终于使用了这个 - https://stackoverflow.com/a/17657154/1086930

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    您可以使用@rajan 答案中提到的layout_weight,但分配不均。例如:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_weight="0.7"
                android:layout_height="wrap_content"
                android:layout_marginRight="8dp"
                android:layout_marginEnd="8dp"
                android:maxLines="1"
                android:text="@string/dummy_paragraph"
                android:textColor="?android:attr/textColorTertiary"
                android:textSize="14sp"/>
    
            <TextView
                android:id="@+id/textView3"
                android:layout_width="0dp"
                android:layout_weight="0.3"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:text="@string/dummy"
                android:textColor="?android:attr/textColorTertiary"
                android:textSize="14sp"/>
    
     </LinearLayout>
    

    或使用RelativeLayout 作为父级并使用对齐方式。例如:

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:layout_marginEnd="8dp"
            android:maxLines="1"
            android:text="@string/dummy_paragraph"
            android:textColor="?android:attr/textColorTertiary"
            android:textSize="14sp"
            android:layout_toLeftOf="@+id/textView3"
            android:layout_alignParentLeft="true" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="@string/dummy"
            android:textColor="?android:attr/textColorTertiary"
            android:textSize="14sp"
            android:layout_alignParentRight="true" />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      为每个 TextView 添加WeightSum="2" 到您的LinearLayoutlayout_weight="1",如

              <TextView
                  android:id="@+id/textView2"
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:layout_marginRight="8dp"
                  android:layout_marginEnd="8dp"
                  android:maxLines="1"
                  android:text="@string/dummy_paragraph"
                  android:textColor="?android:attr/textColorTertiary"
                  android:textSize="14sp"/>
      
              <TextView
                  android:id="@+id/textView3"
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:maxLines="1"
                  android:text="@string/dummy"
                  android:textColor="?android:attr/textColorTertiary"
                  android:textSize="14sp"/>
      
          </LinearLayout>
      

      【讨论】:

      • 我不想要平均分配。我希望第一个 textview 占用所需的宽度,并将其余部分交给第二个 textview。
      • @jaibatrik 如果你的第一个 TextView 占据了所有的布局宽度,第三个文本没有休息,它将在屏幕上不可见。
      • 我相信这里就是这种情况,因为我看到它将另一个TextView 推出其父LinearLayout。你建议我怎么做?
      • 我应该使用RelativeLayout吗?
      • 我选择了 RelativeLayout 和 toLeftOf 属性。对此表示赞同并接受了另一个答案。感谢您的帮助!
      猜你喜欢
      • 2011-11-02
      • 2017-11-11
      • 2014-10-26
      • 1970-01-01
      • 2014-05-04
      • 2020-10-09
      • 2014-01-15
      • 1970-01-01
      • 2020-02-05
      相关资源
      最近更新 更多