【问题标题】:Android Studio: Bottom Navigation View - AAPT: error: ' ' is incompatible with attribute drawable (attr) referenceAndroid Studio:底部导航视图 - AAPT:错误:“”与可绘制属性 (attr) 引用不兼容
【发布时间】:2019-10-23 13:16:36
【问题描述】:

我已经在我的应用程序中添加了 BottomNavigationView。

ma​​in.xml

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="56dp"
    app:itemBackground="@drawable/nav_bgcolor"
    app:itemIconSize="50dp"
    app:itemIconTint="@color/nav_item_colors"
    app:itemTextColor="@color/nav_item_colors"
    app:labelVisibilityMode="unlabeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/nav_items">
</com.google.android.material.bottomnavigation.BottomNavigationView>

menu/nav_items.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/home_white"
        android:title="@string/nav_home" />
    <item
        android:id="@+id/nav_market"
        android:icon="@drawable/market_white"
        android:title="@string/nav_market" />
    <item
        android:id="@+id/nav_news"
        android:icon="@drawable/news_white"
        android:title="@string/nav_news" />
    <item
        android:id="@+id/nav_account"
        android:icon="@drawable/account_white"
        android:title="@string/nav_account" />
</menu>

drawable/nav_bgcolor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="#FF9800" android:state_checked="true" />
    <item android:drawable="#FFFFFF" android:state_checked="false" />
</selector>

color/nav_item_colors.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#FFFFFF" android:state_checked="true"/>
    <item android:color="#727272"/>
</selector>

这是android studio中的预览

https://drive.google.com/file/d/1yICxFcTbSxYpILDtxg75Z4x-2Y1UaPM_/view?usp=sharing

它实际上是在展示我想要实现的设计。

没有错误或其他东西,但是当我运行应用程序时,drawable/nav_bgcolor.xml

中出现错误
C:\Users\[PCNAME]\AndroidStudioProjects\[APPNAME]\app\src\main\res\drawable\nav_bgcolor.xml:3: AAPT: error: '#FF9800' is incompatible with attribute drawable (attr) reference.
C:\Users\[PCNAME]\AndroidStudioProjects\[APPNAME]\app\src\main\res\drawable\nav_bgcolor.xml:4: AAPT: error: '#ffffff' is incompatible with attribute drawable (attr) reference.

我在 ma​​in.xml

中添加 app:itemBackground="@drawable/nav_bgcolor" 后发生错误

我已经搜索了互联网,但没有找到任何解决方案。

【问题讨论】:

    标签: android android-layout bottomnavigationview material-components material-components-android


    【解决方案1】:

    在您正在使用的选择器中

    <selector>
        <item android:drawable="#FF9800" android:state_checked="true" />
    

    您不能在应该定义 android:drawable 的地方使用颜色 (#FF9800)。

    使用

    <menu>
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/icon_add"
            android:title="@string/nav_home" />
        ...
    </menu>
    

    icon_add 是一个简单的可绘制对象。
    在你的BottomNavigationView:

    <com.google.android.material.bottomnavigation.BottomNavigationView
         app:itemIconTint="@color/nav_item_colors"
         ../>
    

    【讨论】:

    • 所以我需要创建一个纯色#FF9800的可绘制资源,然后在可绘制选择器中使用它?
    • @troidevera 你的最终目的是什么?您想要带有选择器的图标色调吗?
    • 我只想在选中时将选中项目的项目背景颜色更改为橙​​色。我已经完成了更改图标色调和文本颜色的选择,即使在运行后也没有错误,但不是 itembackground 因为有这个 AAPT 错误。
    • 这解决了我的问题。谢谢@GabrieleMariotti先生。
    • 只是想问另一个。因为我在 main.xml 的底部导航视图中添加了 android:background ,但它不适用于设计。顺便说一下,我正在添加一个可绘制的背景。我不能同时应用 app:itembackground 和 android:background 吗?因为当我尝试删除 app:itembackground 时,android:background 正在显示。
    【解决方案2】:

    将drawable/nav_bgcolor.xml中的android:drawable替换为android:color

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="#FF9800" android:state_checked="true" />
        <item android:color="#FFFFFF" android:state_checked="false" />
    </selector>
    

    【讨论】:

    • 没有。它用作菜单中的图标。它适用于可绘制而不是颜色选择器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2018-06-13
    • 2020-08-31
    • 2018-06-20
    • 1970-01-01
    相关资源
    最近更新 更多