Android selector 失效的一個原因

來源:互聯網
上載者:User
問題重現:

使用selector,期望按下按鈕時 Button 圖片變色,代碼如下:

    <Button
        android:id="@+id/btn_match"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/btn_match"
        android:text="按我變色" />

其中,selector 具體寫在
btn_match.xml 中,控制各種 state 下按鈕的應有的狀態。

但是實際運行時按鈕並不變色。

原因:

首先,這個不是 pressed、selected、default 的順序不對引起的問題;

真正的原因是,在我的工程的 drawable 檔案夾下有一個名為 btn_match.png 的圖片,系統每次優先將 android:drawableTop="@drawable/btn_match" 中的 btn_match 解析為 btn_match.png,而非預期的 btn_match.xml。

當你按住 Ctrl,將滑鼠移至"@drawable/btn_match"時,會有2個提示句:第一個是btn_match.png,第二個才是 btn_match.xml,系統優先選擇btn_match.png,故沒有變色效果。

解決方案:

刪除 btn_match.png 或將其重新命名即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.