具有视图换行内容,但如果有可用空间,则匹配父项

时间:2018-12-30 15:14:34

标签: android layout

我在LinearLayout中有两个视图,两个视图都应wrap_content,使得它们的最小尺寸足以显示其内容。 LinearLayout视图组应位于两个子组的wrap_content中,以使其足够大以显示两个子视图的内容。

但是,在此之后,如果两个子视图之一较大,则另一个子视图应扩展为与父视图匹配,以填充可用的剩余空间。这两个子视图的内容是动态的,并且是未知的,在运行时会更大。

两个子视图分别是TextViewSpinnerSpinner应该填充LinearLayout宽度中的所有剩余空间。但是,如果我将Spinner layout_width更改为match_parent并且TextView不够大,则Spinner将截断其内容。

基本上,我需要一种在wrap_contentmatch_parent之间选择最大宽度的方法。

这是布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parentView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-2dp"
        android:layout_marginBottom="-2dp"
        android:paddingStart="4dp"
        android:paddingEnd="4dp"
        android:textSize="12sp"
        android:textColor="?android:textColorHint"
        tools:text="Label" />

    <Spinner
        android:id="@+id/spinner"
        style="@style/Widget.AppCompat.Spinner.Underlined"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<!-- or android:layout_width="match_parent"? need max of both... -->

</LinearLayout>

5 个答案:

答案 0 :(得分:1)

您可以使用一种技巧来使用LinearLayout来完成此操作,而无需使用其他视图:在父级LinearLayout上使用wrap_content并在{em孩子们。只要LinearLayout没有尺寸固定的 any 子代,此组合将使整个父代与其最大子代一样宽。

match_parent

enter image description here

enter image description here

(我在LinearLayout中添加了背景色,以使其更容易查看其自身大小。)

答案 1 :(得分:0)

您的LinearLayout宽度为wrap_content的问题。这意味着子代永远不会填满整个宽度,除非内部内容会改变宽度。

只需更改LinearLayout中的行即可。发件人:

android:layout_width="wrap_content"

收件人

android:layout_width="match_parent"

答案 2 :(得分:0)

尝试设置布局权重android:layout_weight。这是SO的定义:https://stackoverflow.com/a/3996104/8738574。 将微调框的权重设置为大于textView的权重,并将其宽度设置为0(零)。

TextView:

android:layout_weight="1"
android:layout_width="0px"

微调框:

android:layout_weight="2"
android:layout_width="0px"

答案 3 :(得分:0)

更新:查看我的其他答案以寻求更好的解决方案。

我希望有一个更优雅的解决方案,但是我实现此目的的方法是添加一个额外的隐藏重复项Spinner,该视图仅用于调整父级LinearLayout的大小,以实现二者之一的最大值wrap_contentmatch_parent,以较大者为准。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parentView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-2dp"
        android:layout_marginBottom="-2dp"
        android:paddingStart="4dp"
        android:paddingEnd="4dp"
        android:textSize="12sp"
        android:textColor="?android:textColorHint"
        tools:text="Label" />

    <Spinner
        android:id="@+id/spinner"
        style="@style/Widget.AppCompat.Spinner.Underlined"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <!-- visible view width set to match_parent -->

    <Spinner
        android:id="@+id/spinnerHiddenSizer"
        style="@style/Widget.AppCompat.Spinner.Underlined"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:visibility="invisible" />
    <!-- invisible view width set to wrap_content -->
    <!-- also set height to 0dp so it takes up no vertical space -->
    <!-- in code, set this Spinner's adapter to the same as -->
    <!-- the visible spinner so that it's sized the same -->

</LinearLayout>

这样,如果wrap_content会使Spinner的宽度大于match_parent,则隐藏视图将导致LinearLayout调整为更大的尺寸可见的match_parent宽度将扩展到该宽度。

答案 4 :(得分:0)

更新:Ben P的答案在仍使用LinearLayout的情况下完成了此操作。

在进一步测试了不同的解决方案之后,发现更好的解决方案是不使用LinearLayout作为父视图组。无论出于何种原因,即使将子视图设置为FrameLayoutmatch_parent也会提供包装子视图内容的所需行为,从而使用两个大小的最大值。 LinearLayout没有提供相同的行为。

但是接下来的问题是如何使两个子视图垂直堆叠。这可以在FrameLayout中结合使用layout_gravitySpace视图来实现。出于某种奇怪的原因,至少在我使用此布局作为FlexboxLayout父级中的项目的用例中,显式设置FrameLayout的高度无法确定其高度。在将wrap_content视图设置为所需高度的情况下使用Space确实可以。

这是一种有效的布局,不需要任何其他代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parentView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!-- setting FrameLayout layout_height doesn't work for some reason -->
    <Space
        android:layout_width="0dp"
        android:layout_height="56dp" />

    <TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|start"
    android:layout_marginTop="-2dp"
    android:layout_marginBottom="-2dp"
    android:paddingStart="4dp"
    android:paddingEnd="4dp"
    android:textSize="12sp"
    android:textColor="?android:textColorHint"
    tools:text="Label" />

    <Spinner
        android:id="@+id/spinner"
        style="@style/Widget.AppCompat.Spinner.Underlined"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />

</FrameLayout>
相关问题