【问题标题】:layout_marginBottom not working inside ConstraintLayoutlayout_marginBottom 在 ConstraintLayout 中不起作用
【发布时间】:2018-07-05 17:34:26
【问题描述】:

我有下面列出的 Android 布局,但 layout_marginBottom 不起作用 - myTextViewmyRecyclerView 之间没有任何边距。

任何想法,我的布局可能有什么问题?

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TextView
        android:id="@+id/myTextView"
        android:text="*** Descritpion text"
        app:layout_constraintWidth_default="spread"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/myRecyclerView"
        android:layout_marginBottom="300dp" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/myRecyclerView"
        app:layout_constraintTop_toBottomOf="@+id/myTextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintWidth_default="spread"
        app:layout_constraintHeight_default="wrap"
        android:layout_width="0dp"
        android:layout_height="0dp" />
</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 您使用的是哪个版本的ConstraintLayout
  • @Cheticamp 1.1.0
  • 我有一些非常相似的地方,没有添加顶部和底部边距。当在链中或不在链中时。我正在使用 1.1.2,ui 设计器显示它很好,但是当构建并推送到应用程序时......它消失了,尤其是在使用包含布局时

标签: android-constraintlayout


【解决方案1】:

您的Views 创建一个默认样式为CHAIN_SPREAD 的链,因为没有为链的头部指定layout_constraintVertical_chainStyle 属性。根据ConstraintLayout 文档:

链中的边距

如果在连接上指定了边距,它们将 予以考虑。在价差链的情况下,保证金将为 从分配的空间中扣除。

【讨论】:

    【解决方案2】:

    请检查以下内容。

    <?xml version="1.0" encoding="utf-8"?>
    <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:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp">
    
        <TextView
            android:id="@+id/myTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="*** Descritpion text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="300dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/myTextView" />
    </android.support.constraint.ConstraintLayout>
    

    更新:

    <?xml version="1.0" encoding="utf-8"?>
    <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:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp">
    
        <TextView
            android:id="@+id/myTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="*** Descritpion text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/myRecyclerView"
            android:layout_marginBottom="300dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/myTextView" />
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 其实这不是我真正需要的,因为有时我隐藏了 TextView 并希望边距也随着 TextView 消失
    • @AlexanderLysenko Okey,明白了。请检查更新的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多