我在 a 中有两个视图LinearLayout,这两个wrap_content视图都应该足够大以显示其内容。视图LinearLayout组应该wrap_content由两个子组组成,使其足够大以显示两个子视图的内容。
但在此之后,如果两个子视图之一较大,则另一个子视图应扩展以匹配父视图,以填充可用的剩余空间。两个子视图的内容是动态的且未知,运行时会变大。
两个子视图是 aTextView和 a Spinner。应Spinner填充宽度上的任何剩余空间LinearLayout。但如果我将 更改Spinner layout_width为match_parent且TextView不够大,则会Spinner截断其内容。
wrap_content基本上我需要一种方法来选择和之间的最大宽度match_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>
Run Code Online (Sandbox Code Playgroud)
您可以使用一个技巧来完成此任务,LinearLayout无需额外的视图:wrap_content在父 LinearLayout 和两个子级match_parent上使用。只要 LinearLayout 没有任何固定大小的子级,这种组合就会使整个父级与其最宽的子级一样宽。
<LinearLayout
android:layout_width="wrap_content"
...>
<TextView
android:layout_width="match_parent"
.../>
<Spinner
android:layout_width="match_parent"
.../>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
(我向 LinearLayout 添加了背景颜色,以便更容易地看到它如何调整自身的大小。)
| 归档时间: |
|
| 查看次数: |
5260 次 |
| 最近记录: |