Android 为什么marginBottom不是';你在纺纱机工作吗?

Android 为什么marginBottom不是';你在纺纱机工作吗?,android,spinner,android-spinner,margins,Android,Spinner,Android Spinner,Margins,有人能解释一下为什么android:layout_marginBottom在旋转器中不起作用吗 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:

有人能解释一下为什么android:layout_marginBottom在旋转器中不起作用吗

<?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>


谢谢。

将约束从按钮连接到“视图”或“父对象”,然后它将影响
$
app:layout_constraintBottom_toBottomOf=“”`

您需要将微调器约束到底部:

第一种方法:
app:layout\u constraintBottom\u toBottomOf=“parent”

第二种方法:
通过将微调器拖动到根视图的底部,将其约束到底部

问题并不是具体的
微调器
——如果使用两个
文本视图
s,行为将保持不变

关于“为什么”和“如何”的一些意见:

  • 微调器
    顶部和起点被约束到父视图组
的顶部和起点。由于未指定底部(或末端)约束,因此底部边距没有意义

  • 另一方面,TextView有一个顶部约束-如果您让它有一个顶部边距,这将达到所需的效果

  • 现在您可以说“好吧,那么我将只向
    微调器添加底部约束”。不幸的是,这还不够(我真的不知道为什么
    ConstraintLayout
    解算器决定忽略边距…)

    如果要为
    微调器设置边距,则两个
    视图必须属于完整的垂直链:

    父上父下

    这是相关的
    <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>