android,我怎样才能使视图的宽度为包装内容,最大宽度为50%

时间:2016-02-22 10:06:23

标签: android xml

我有一个布局,我必须在同一行显示两个文本视图,如果文本视图中的文本很短,它们应该是换行内容并显示在另一个旁边,如果文本很长,则textviews应该扩展但不再是父母宽度的50%。我的问题是我不知道如何将最大宽度设置为父级的50%

1 个答案:

答案 0 :(得分:0)

LinearLayout方向使用horizontal

  

示例代码。

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


    <TextView
        android:id="@+id/tv_one_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="key"
        android:layout_weight="1.4"
        android:textAllCaps="false"
        />
 <TextView
        android:id="@+id/tv_separater"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_weight="0.2"
        android:textAllCaps="false"
        />
    <TextView
        android:id="@+id/tv_two_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Value"
        android:layout_weight="1.4"
        android:textAllCaps="false"
        />
</LinearLayout>