selectableItemBackground as item in layer-list

不打扰是莪最后的温柔 提交于 2019-12-04 09:57:02

问题


I've copied a file from the Google IO Schedule app source (https://github.com/google/iosched) namely

selected_navdrawer_item_background.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/backgroundColor">
        <shape>
            <solid android:color="#12000000" />
        </shape>
    </item>
    <item android:drawable="?android:selectableItemBackground"/>
</layer-list>

I want to use this to highlight the currently selected item in a NavigationDrawer. My problem is, when I launch my app, it throws an exception.

This is the important line I think.

caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #22:
<item> tag requires a 'drawable' attribute or child tag defining a drawable

Line # 22 is this one

<item android:drawable="?android:selectableItemBackground"/>

I don't know what the problem is, I copied this over from source without adjusting it. It's working fine in their app.

I tried to change ?android:selectableItemBackground to ?attr/selectableItemBackground, but it gives me the same exception. I can't seem to find any other suggested solutions.

If anyone knows what's causing this, please help me.


回答1:


It works but from api v21. So, you can go ahead and use it on devices with Android Lollipop and newer. If you want support older system versions you can put this xml into drawable-v21 folder and prepare new xml in drawable folder with the same name. For example:

drawable/selected_navdrawer_item_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
     <solid android:color="#12000000" />
</shape> 

Google IO app does the same thing: here is file from drawable folder and here is file from drawable-v21 folder



来源:https://stackoverflow.com/questions/28432686/selectableitembackground-as-item-in-layer-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!