背景有问题:@ drawable / img

时间:2018-03-04 11:21:00

标签: android android-layout

我是Android的初学者。我刚刚编写了代码,当我在LinearLayoutCollapsingToolbarLayout上插入我的图片时,这样:

android:background=@drawable/myimage

我收到此错误:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.nanda.kosku/com.example.nanda.kosku.DetailBangunanActivity}:
android.view.InflateException: Binary XML file line #0: Binary XMLfile line #0: Error inflating class android.support.design.widget.CollapsingToolbarLayout

这是我的代码:

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsinglayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_scrollFlags="exitUntilCollapsed|scroll"
    app:contentScrim="@color/colorPrimary"
    app:title="App Title"
    app:expandedTitleMarginStart="48dp"
    app:expandedTitleMarginEnd="64dp"
    android:background="@drawable/bg">

    <android.support.v7.widget.Toolbar
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_collapseMode="pin"
        android:id="@+id/toolbarid"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

</android.support.design.widget.CollapsingToolbarLayout>

1 个答案:

答案 0 :(得分:0)

您需要在应用内dependencies文件的build.gradle部分下添加以下内容。

dependencies {
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    comple 'com.android.support:appcompat-v7:25.3.1'

    //... Other dependencies
}

最终布局应如下所示。

<android.support.design.widget.CollapsingToolbarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/collapsinglayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    app:contentScrim="@color/colorPrimary"
    app:expandedTitleMarginEnd="64dp"
    app:expandedTitleMarginStart="48dp"
    app:layout_scrollFlags="exitUntilCollapsed|scroll"
    app:title="App Title">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarid"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

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

</android.support.design.widget.CollapsingToolbarLayout>