2010-08-17 36 views
11

當用戶按下ListView項(android:state_pressed =「true」)時,它會閃爍一個黃色陰影(或者您可以按住)。什麼是按下列表項的默認可繪製方式

這是什麼可拉?我創建了自己的選擇器,因爲我想要自己的ListView項目的顏色,但是我失去了按下的顏色。

有一個關於皮膚按鈕引用#ffff0000的Android文檔,但這會產生紅色。

有誰知道它是什麼以及如何引用它?

回答

36

默認的系統資源可以在<android-sdk>/platforms/android-<version>/data/res找到。特別地,該列表選擇器在drawable/list_selector_background.xml定義:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_window_focused="false" 
     android:drawable="@color/transparent" /> 

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. --> 
    <item android:state_focused="true" android:state_enabled="false" 
     android:state_pressed="true" 
     android:drawable="@drawable/list_selector_background_disabled" /> 
    <item android:state_focused="true" android:state_enabled="false" 
     android:drawable="@drawable/list_selector_background_disabled" /> 

    <item android:state_focused="true" android:state_pressed="true" 
     android:drawable="@drawable/list_selector_background_transition" /> 
    <item android:state_focused="false" android:state_pressed="true" 
     android:drawable="@drawable/list_selector_background_transition" /> 

    <item android:state_focused="true" 
     android:drawable="@drawable/list_selector_background_focus" /> 

</selector> 

,其上一按,list_selector_background_transition所示的可繪製的,不是單一的顏色,但兩個9補丁圖像,黃色和白色的,具有一個動畫之間的轉換。

<transition xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@android:drawable/list_selector_background_pressed" /> 
    <item android:drawable="@android:drawable/list_selector_background_longpress" /> 
</transition> 
+2

簡而言之:你想引用@android:drawable/list_selector_background_transition – Jonas 2010-12-02 19:27:28

+0

當我構建自己的選擇器時,爲什麼我不能引用@android:drawable/list_selector_background_transition? – Andrew 2010-12-08 16:42:25

+0

我也注意到了。總之,我不知道爲什麼。但是,您可以在您的項目中創建自己的list_selector_background_transition副本。然後你可以參考那個。 – Jonas 2011-01-18 18:55:59

1

顏色被定義爲#AARRGGBB其中AA表示alpha(透明度)值,RR表示紅色的量,GG表示綠色的量,BB表示藍色的量。因此#ffff0000是堅實的並且全是紅色的。如果你想要橙色,你想添加一些綠色,例如:#ffffA500。 Google for RGB顏色值可以用rgb值查看顏色頁面。

+1

是的,我有這個。我只是想知道Android默認。但是,感謝您的迴應 – Andrew 2010-08-17 21:22:46

2

你說的是Android OS內置選擇器。

使自己的亮點與您繪製-夾中的XML文件是這樣的:

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

然後在你的XML文件有你有你的ListView。

android:textColor="@drawable/highlight" //For text to appear like YOURCOLOR 
//or if you wish the background 
android:background="@drawable/highlight" //For the background to appear like YOURCOLOR 

我希望這是它,並告訴我,如果這工作或沒有!

+1

感謝您的回覆。我已經創建了這樣的選擇器文件。我用羅曼蓋伊的技巧來維護一個滾動鍵選擇器,我已經設置了背景顏色的列表項。我正在尋找的是Android默認的state_pressed顏色。 – Andrew 2010-08-17 21:24:53

+0

你可以做一個醜陋的黑客打印按下的項目,然後查找RGB顏色,但當然,默認顏色是在某處聲明。 問題是如何找到它 – Curtain 2010-08-17 21:27:55

+0

它實際上看起來更像是一些價值的梯度 – Andrew 2010-08-17 21:29:58

0

我在照片編輯應用程序中使用了顏色選擇工具來查找漸變的外部和內部顏色,並創建了自己的顏色。

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#ffb300" 
     android:centerColor="#ffc800" 
     android:endColor="#ffb300" 
     android:angle="270"/> 
</shape> 
相關問題