如何解决为什么 marginBottom 在 Spinner 中不起作用?
有人能解释一下为什么 android:layout_marginBottom
在 Spinner 中不起作用吗?:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="match_parent">
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginBottom="20dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner2" />
</androidx.constraintlayout.widget.ConstraintLayout>
我使用 android:layout_marginBottom
还是 android:layout_margin
都没有关系。但是,如果我使用 android:layout_margin
,它会添加顶部、右侧和左侧边距。为什么唯一不起作用的边距是底部?
谢谢。
解决方法
从按钮连接约束到视图或父项然后它会影响
$
app:layout_constraintBottom_toBottomOf="" `
您需要将微调器约束到底部:
第一种方法:
app:layout_constraintBottom_toBottomOf="parent"
第二种方法: 将微调器拖动到根视图的底部,将其约束到底部
,问题不在于具体的 Spinner
- 如果您使用两个 TextView
,则行为保持不变。
关于“为什么”和“如何”的一些观察:
-
Spinner
的顶部和开始被限制在父ViewGroup
的顶部和开始。由于您没有指定底部(或结束)约束,因此底部边距毫无意义 -
另一方面,TextView 有一个顶部约束——如果你让它有一个顶部边距,这将产生预期的效果。
现在您可以说“好吧,那么我将向 Spinner
添加底部约束”。不幸的是,这还不够(我真的不知道为什么ConstraintLayout
求解器决定忽略边距...)
如果要为 Spinner
设置外边距,则两个 View
必须属于一个完整的垂直链:
父顶部 TextView -> 父底部
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/textView"
android:layout_marginBottom="20dp"
app:layout_constraintVertical_bias="0"
app:layout_constraintVertical_chainStyle="packed"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner2"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。