cardView看起来不正确,卡片受到限制

时间:2018-03-29 20:24:42

标签: java android xml android-studio

This is the problem , when i'm runing it on the phone it's looks like that

这是我用于每个“行”的卡的代码,我尝试了几种方法,但没有一个工作。每次在手机上运行时它都会受到限制。还有另一种解决方法吗?

alert

这是recyclerView主要活动布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    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:layout_marginTop="8dp">

    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="63dp"
            android:layout_marginStart="26dp"
            android:layout_marginTop="11dp"
            android:text="TextView"
            app:layout_constraintEnd_toStartOf="@+id/textView9"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="12dp"
            android:layout_marginTop="11dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/textView10"
            app:layout_constraintStart_toEndOf="@+id/textView8"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="19dp"
            android:layout_marginTop="11dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/textView11"
            app:layout_constraintStart_toEndOf="@+id/textView9"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="11dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/textView10"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

CustomViewHolder的代码

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="397dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">
</android.support.v7.widget.RecyclerView>

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题:您的RecyclerView位于ConstraintLayout内,而RecyclerView的宽度是通过将其左侧和右侧约束为父级来定义的。由于某种原因(我不确定为什么),这会导致适配器在创建第一批视图时无法确定父级的宽度,因此项目视图在功能上wrap_content而不是{{ 1}}为宽度。

您可以通过在match_parent以外的某个视图组中托管RecyclerView来解决此问题。如果这不是你的选择,我不确定如何继续。