簡體   English   中英

Android:如何制作可繪制的選擇器

[英]Android: How to Make A Drawable Selector

我覺得這是一個愚蠢的問題,但無論如何我都在這里。 我有一個圖像按鈕,我希望能夠在每次單擊時更改它的圖像。 API 似乎說最好的方法是在包含選擇器和值的 drawable 文件夾中創建 xml 資源。 但是,當我去創建一個新的 android xml 資源時,drawables 沒有選項。 我錯過了什么?

您可以在 Android Studio 中添加它,使用Right click on project structure -> New -> Drawable resource file 它應該是這樣的:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" 
        android:drawable="@drawable/cell_top_selected" />
    <item android:drawable="@drawable/cell_top" />
</selector>

你也可以試試這個作為選擇器

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

    <!-- pressed -->
    <item android:drawable="@drawable/button_1_selected" android:state_pressed="true"/>
    <!-- focused -->
    <item android:drawable="@drawable/button_1_normal" android:state_focused="true"/>
    <!-- default -->
    <item android:drawable="@drawable/button_1_normal"/>

</selector>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM