【问题标题】:app:layout_marginBottom is not working well with android constraint layoutapp:layout_marginBottom 不适用于 android 约束布局
【发布时间】:2017-12-09 10:10:27
【问题描述】:

下面的 layout_marginBottom 是否有任何原因不起作用? 但是,如果我在第二个视图上使用 layout_marginTop 效果很好

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ade4ad">
    <TextView
        android:id="@+id/first"
        android:layout_width="90dp"
        android:layout_height="40dp"
        app:layout_marginBottom="10dp"
        android:background="#000"/>
    <TextView
        android:id="@+id/second"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:background="#fff"
        app:layout_constraintTop_toBottomOf="@+id/first"/>
</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 我添加了完全相同的问题,并通过向您的第一个 textView 添加以下约束来修复它:app:layout_constraintBottomToBottomOf="@+id/second"。然后 marginBottom 运行良好
  • 你需要有一个约束才能有一个边距。例如,对于底部边距,底部约束是必须的。其他方面也类似。
  • 感谢 shiva,这是正确的答案。

标签: android margin android-constraintlayout


【解决方案1】:

这不是LinearLayoutRelativeLayout,而是ConstraintLayout,所以你必须将LeftRightBottomTopConstraint 提供给相关布局,在你的情况下你有给TextView 第一个Bottom_Top 约束到TextView 第二个。所以你可以获得两个TextView之间的保证金。

您的布局应该如下所示。

<android.support.constraint.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"
    android:background="#ade4ad">
    <TextView
        android:id="@+id/first"
        android:layout_width="90dp"
        android:layout_height="40dp" 
        android:background="#000"
        app:layout_constraintTop_toTopOf="parent" 
        app:layout_constraintLeft_toLeftOf="parent" />
    <TextView
        android:id="@+id/second"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:background="#fff"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@+id/first" 
        app:layout_constraintLeft_toLeftOf="@+id/first" 
        app:layout_constraintRight_toRightOf="@+id/first" />
</android.support.constraint.ConstraintLayout>

【讨论】:

  • 您的解决方案没有解释为什么第一个视图上的 android:layout_marginBottom="10dp" 不起作用(也适用于您的代码)。
【解决方案2】:

为了

android:layout_marginBottom="20dp" 

工作得很好,你应该使用

app:layout_constraintBottom_toBottomOf="parent"

【讨论】:

  • 如果您希望顶部和底部边距得到尊重,您必须对所有视图的顶部和底部应用约束。左右约束以及左右边距也是如此。
【解决方案3】:

你可以使用这个技巧,在下面创建一个空格,与父底部对齐

<Space
   android:id="@+id/space"
   android:layout_width="wrap_content"
   android:layout_height="80dp"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintStart_toStartOf="parent" />

并在空间顶部对齐您的视图 应用程序:layout_constraintBottom_toTopOf="@+id/space" 像这样

<TextView
    android:id="@+id/howNext"
    style="@style/white_action_btn_no_border"
    android:layout_width="344dp"
    android:layout_height="60dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="@string/got_it_next"
    app:layout_constraintBottom_toTopOf="@+id/space"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

【讨论】:

    【解决方案4】:

    布局顶部/底部边距仅在以下情况下有效:

    1. 同一方向的约束需要与其下一个邻居子节点连接,就像单向链表一样。
    2. 必须设置方向的最后一个约束。

    在您的情况下,您需要为链中的每个视图设置“layout_constraintBottom_toXXXXX”,最后一个视图将底部设置为父视图。

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ade4ad">
        <TextView
            android:id="@+id/first"
            android:layout_width="90dp"
            android:layout_height="40dp"
            app:layout_marginBottom="10dp"
            app:layout_constraintBottom_toTopOf="@+id/second"
            android:background="#000"/>
        <TextView
            android:id="@+id/second"
            android:layout_width="90dp"
            android:layout_height="40dp"
            app:layout_marginBottom="10dp"
            app:layout_constraintBottom_toTopOf="@+id/third"
            android:background="#fff"/>
        <TextView
            android:id="@+id/third"
            android:layout_width="90dp"
            android:layout_height="40dp"
            android:background="#fff"
            app:layout_constraintBottom_toBottomOf="parent"/>
    </android.support.constraint.ConstraintLayout>
    

    此外,不需要反向依赖,除非您希望“layout_marginTop”有效。

    【讨论】:

    • 知道了。简单地说:视图的约束只尊重视图自己定义的边距。
    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    相关资源
    最近更新 更多