错误:错误:“ drawable / delete.png”与属性android:background(attr)reference | color

时间:2019-02-26 07:15:46

标签: android xml android-layout

当我尝试在活动的XML文件中更改android:background时遇到此问题。

Information:Gradle tasks [:app:assembleDebug]
C:\Users\Valentin\AndroidStudioProjects\CedarRestaurants4\CedarRestaurants3\app\src\main\res\layout\activity_dynamic_view_ordering.xml
Error:error: 'drawable/delete' is incompatible with attribute android:background (attr) reference|color.
Error:'drawable/delete' is incompatible with attribute android:background (attr) reference|color.
Error:failed linking file resources.
Error:java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:Execution failed for task ':app:processDebugResources'.
> Failed to execute aapt
Information:BUILD FAILED in 4s
Information:7 errors
Information:0 warnings
Information:See complete output in console

我的活动的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >
        <EditText
            android:id="@+id/number_edit_text"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="drawable/delete.png"
            android:onClick="onDelete" />
    </LinearLayout>
    <Button
        android:id="@+id/add_field_button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#555"
        android:layout_gravity="center"
        android:onClick="onAddField"
        android:textColor="#FFF"
        android:text="New"
        android:paddingLeft="5dp"/>
</LinearLayout>

<Button
    android:id="@+id/placeOrderButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/parent_linear_layout"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="250sp"
    android:text="@string/place_order"
    android:textSize="32sp" />


</RelativeLayout>

我从一个教程项目中复制了活动布局文件和活动,并试图将图像资产更改为更现代的外观。

我已经确保我的delete.png位于/src/main/res/drawable内,并且已经尝试在最后摆脱.png,但是没有运气。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

您忘记将@添加到drawable并删除了extention。因此,更改您的

android:background="drawable/delete.png"

android:src="@drawable/delete"

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >
        <EditText
            android:id="@+id/number_edit_text"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="drawable/delete"
            android:onClick="onDelete" />
    </LinearLayout>
    <Button
        android:id="@+id/add_field_button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#555"
        android:layout_gravity="center"
        android:onClick="onAddField"
        android:textColor="#FFF"
        android:text="New"
        android:paddingLeft="5dp"/>
</LinearLayout>

<Button
    android:id="@+id/placeOrderButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/parent_linear_layout"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="250sp"
    android:text="@string/place_order"
    android:textSize="32sp" />


</RelativeLayout>