[英]Why marginBottom isn't working in Spinner?
Could someome explain me why android:layout_marginBottom
doesn't work in a 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>
It doesn't matter if I use android:layout_marginBottom
or android:layout_margin
.我使用
android:layout_marginBottom
还是android:layout_margin
。 However, If I use android:layout_margin
it adds top, right and left margin.但是,如果我使用
android:layout_margin
它会添加顶部、右侧和左侧边距。 Why the only margin that does't work is the bottom?为什么唯一不起作用的边距是底部?
Thanks.谢谢。
connect constrain from the button to view or parent then it will affect $
app:layout_constraintBottom_toBottomOf="" `将约束从按钮连接到视图或父级,然后它将影响
$
app:layout_constraintBottom_toBottomOf="" `
You need to constraint the spinner to bottom:您需要将微调器限制在底部:
1st method: app:layout_constraintBottom_toBottomOf="parent"
第一种方法:
app:layout_constraintBottom_toBottomOf="parent"
2nd method: constraint the spinner to bottom by drag it until the bottom of your root view第二种方法:通过将微调器拖动到根视图的底部来将其约束到底部
The problem is not about Spinner
specifically - if you use two TextView
s the behavior stays the same.问题不在于
Spinner
- 如果您使用两个TextView
,则行为保持不变。
Some observations on "why" and "how":关于“为什么”和“如何”的一些观察:
the Spinner
top and start is constrained to the top and start of the parent ViewGroup
. Spinner
top 和 start 被限制在父ViewGroup
的 top 和 start 上。 Since you don't specify a bottom (or end) constraint, a bottom margin is meaningless由于您没有指定底部(或结束)约束,因此底部边距是没有意义的
the TextView on the other hand has a top constraint - if you let it have a top margin, this will have the desired effect.另一方面,TextView 有一个顶部约束——如果你让它有一个顶部边距,这将达到预期的效果。
Now you could say "well, then I'll just add the bottom constraint to the Spinner
".现在你可以说“好吧,那我就将底部约束添加到
Spinner
”。 Unfortunately this is not enough (and here I really don't know why the ConstraintLayout
solver decides to ignore the margin...)不幸的是,这还不够(这里我真的不知道为什么
ConstraintLayout
求解器决定忽略边距......)
If you want to set a margin to the Spinner
, then the two View
s have to belong to a complete vertical chain:如果
Spinner
设置边距,那么两个View
必须属于一个完整的垂直链:
parent top <- Spinner <-> TextView -> parent bottom父顶部 <- 微调器 <-> 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.