如何居中约束布局并添加填充

时间:2020-05-22 02:52:16

标签: android android-constraintlayout

我使用Recyclerview,如您所见,它的左对齐并且有no margin before the first card,最后一张卡片(last card button not shown)之前没有底边距,我该如何解决?我在卡上添加了填充,但对我无效 我还在约束布局上添加了边距,但第一张卡始终停留在顶部。

XML代码

<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="wrap_content"
android:layout_height="wrap_content"
android:background="@color/design_default_color_surface"
app:layout_constraintHeight_max="wrap"
app:layout_constraintWidth_max="wrap">

<com.google.android.material.card.MaterialCardView
    android:id="@+id/card"
    android:layout_width="270dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- Media -->

        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/iconfinder_artboard_1_1790655" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="title"
            android:textAppearance="?attr/textAppearanceHeadline6" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="secondary_text"
            android:textAppearance="?attr/textAppearanceBody2"
            android:textColor="?android:attr/textColorSecondary" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/readMore"
            style="?attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            android:text="Read More" />


    </LinearLayout>

</com.google.android.material.card.MaterialCardView>


</androidx.constraintlayout.widget.ConstraintLayout>

Recyclerview XML

<?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"
tools:context=".AllBooksRecycler">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler"
    android:layout_width="393dp"
    android:layout_height="717dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
   </androidx.constraintlayout.widget.ConstraintLayout>

2 个答案:

答案 0 :(得分:1)

在RecyclerView标记中,将layout_widthlayout_height都设置为match_parent,而不是对其进行硬编码。

从项目布局中删除这两行:

app:layout_constraintHeight_max="wrap"
app:layout_constraintWidth_max="wrap"

答案 1 :(得分:0)

您不应该修复容器的最大高度和宽度,而删除app:layout_constraintHeight_max="wrap"app:layout_constraintWidth_max="wrap"的尝试

<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="wrap_content"
android:background="@color/design_default_color_surface">

<com.google.android.material.card.MaterialCardView
    android:id="@+id/card"
    android:layout_width="270dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- Media -->

        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/iconfinder_artboard_1_1790655" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="title"
            android:textAppearance="?attr/textAppearanceHeadline6" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="secondary_text"
            android:textAppearance="?attr/textAppearanceBody2"
            android:textColor="?android:attr/textColorSecondary" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/readMore"
            style="?attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            android:text="Read More" />


    </LinearLayout>

</com.google.android.material.card.MaterialCardView>


</androidx.constraintlayout.widget.ConstraintLayout>

对于顶部和底部的第一个和最后一个项目,您应该使用ItemDecoration(这是科特林代码)

class SpacingItemDecoration(context: Context) : RecyclerView.ItemDecoration() {
    // define your dimens you want in dimes.xml
    private val spacing = context.resources.getDimensionPixelSize(R.dimen.your_dimen)

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        val position = parent.getChildAdapterPosition(view)
        outRect.top = spacing
        if (position.inc() == parent.adapter!!.itemCount) {
            outRect.bottom = spacing
        }
    }
}

在初始化RecyclerView时添加它

recycler_view.addItemDecoration(SpacingItemDecoration(context))