【发布时间】:2019-06-12 12:10:13
【问题描述】:
我的应用中有一个Spinner,带有自定义的下拉视图。这是下拉项的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/leadingButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_weight="0" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/dropdown_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/dropdown_text_subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dropdown_text" />
</RelativeLayout>
</FrameLayout>
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/trailingButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_weight="0" />
</LinearLayout>
Android Studio 警告我我的FrameLayout 没用。但是当我取出FrameLayout 时,下拉视图会变窄,并且不再与微调器本身对齐。当我尝试用ConstraintLayout 重写下拉项目时遇到了同样的问题:下拉列表变窄,大约是 Spinner 大小的一半,并且无法显示所有文本,即使 ConstraintLayout 有 @ 987654329@.
一个草图来说明我的意思:
为什么会这样?如何根据布局预测下拉菜单的宽度?
我发现这个下拉视图的大小非常神奇
【问题讨论】:
-
微调器在哪里?
-
无论如何我都会使用 ConstraintLayout,由于嵌套,您的布局很难在视觉上导航;您还使用带有权重的 LinearLayout;这对性能非常不利,并且不会增加太多价值。您的布局中没有什么是 ConstraintLayout 不可能实现的。
-
@MartinMarconcini 我知道为什么 ConstraintLayout 很好。这个问题与此无关。本题是关于Android布局系统如何确定spinner下拉项的宽度的。
标签: android android-layout android-view android-spinner android-measure