让CoordinatorLayout使用下面的Header和RecyclerView

时间:2017-10-16 17:03:20

标签: android android-recyclerview android-coordinatorlayout

它不想让我的CoordinatorLayout膨胀。 Logcat抱怨:

java.lang.RuntimeException: Unable to start activity ComponentInfo{....}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class CoordinatorLayout

我已经搜索了溢出并在build.gradle中添加了设计库依赖项,但它仍然抱怨。是什么让它做到这一点,我该如何解决它?

<?xml version="1.0" encoding="utf-8"?>
<CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <include
        android:id="@+id/primary_info"
        layout="@layout/primary_detail_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:background="@color/colorPrimaryDark"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:paddingStart="16dp"

        android:textColor="@android:color/white"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Test text"
        tools:text="Chappie" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_movie_details"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="8dp"/>    
</CoordinatorLayout>

的build.gradle:

    dataBinding.enabled = true
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.0.0'
    compile 'com.android.support:recyclerview-v7:26.0.0'
    compile 'com.firebase:firebase-jobdispatcher:0.5.0'

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.0'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
}

1 个答案:

答案 0 :(得分:1)

添加到build.gradle implementation 'com.android.support.constraint:constraint-layout:1.0.2'implementation 'com.android.support:design:26.1.0'并同步项目

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <include
        android:id="@+id/primary_info"
        layout="@layout/primary_detail_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:background="@color/colorPrimaryDark"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:paddingStart="16dp"

        android:textColor="@android:color/white"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Test text"
        tools:text="Chappie" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_movie_details"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="8dp"/>
</android.support.design.widget.CoordinatorLayout>