首页 > 解决方案 > AAPT:错误:“6495ed”与属性背景 (attr) 参考不兼容|颜色

问题描述

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:layout_marginTop="15dp">

    <View
        android:id="@+id/divider2"
        android:layout_width="wrap_content"
        android:layout_height="5dp"
        android:background="6495ed"/>


</LinearLayout>

这是我的代码,它一直显示与属性背景不兼容的错误

标签: javaandroid-studio

解决方案


android:background="6495ed"是问题所在。你希望它写成android:background="#6495ed"

更好的做法是将这些颜色值保存在专门保存颜色的 XML 文件中。这些位于您的 res>values 文件夹中,位于 colors.xml 下。

为此,请在 res>values>colors.xml 中添加:

<color name="addala_color_blue">#6495ed</color>

然后在您的布局 XML 中,您将设置:

android:background="@color/addala_color_blue"或任何你命名的东西。


推荐阅读