【问题标题】:Margin below <TextView/> in <ConstraintLayout/> not working<ConstraintLayout/> 中 <TextView/> 下方的边距不起作用
【发布时间】:2017-11-15 01:10:44
【问题描述】:

有一个简单的布局,TextViewRecyclerViewConstraintLayout 内。

    <android.support.constraint.ConstraintLayout
        android:id="@+id/clSelectedPatient"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvSelectPatient"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Lifcare"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_marginBottom="100dp"
            app:layout_constraintBottom_toTopOf="@+id/rvPatients"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvPatients"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            app:layout_constraintTop_toBottomOf="@+id/tvSelectPatient"/>

    </android.support.constraint.ConstraintLayout>

TextView 下面的android:layout_marginBottom="100dp" 不起作用。

【问题讨论】:

    标签: android xml android-recyclerview android-constraintlayout


    【解决方案1】:

    你的布局有几个错误:

    • 您使用 match_parent 作为宽度,并且禁止对 ConstraintLayout 子视图使用 match_parent。

    Build a Responsive UI with ConstraintLayout - Adjust the view size

    注意:您不能将 match_parent 用于 ConstraintLayout 中的任何视图。 而是使用“匹配约束”(0dp)

    • 为了正确显示垂直边距,您必须为 TextView 和 RecyclerView 定义垂直约束。

    您的固定布局代码如下所示:

    <android.support.constraint.ConstraintLayout
        android:id="@+id/clSelectedPatient"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/tvSelectPatient"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="100dp"
            android:text="Lifcare"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/rvPatients" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvPatients"
            android:layout_width="0dp"
            android:layout_height="100dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tvSelectPatient"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    这就是它在设备上的样子:

    【讨论】:

      【解决方案2】:

      添加

      app:layout_constraintBottom_toBottomOf="parent"

      到你的 RecyclerView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-12
        • 2018-03-16
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 1970-01-01
        • 1970-01-01
        • 2012-03-07
        相关资源
        最近更新 更多