2014-07-09 22 views
2

我無法圍繞這個包裹我的頭,我想將我的列表視圖項目的初始背景顏色設置爲灰色,但是隻有在我擁有了正確的背景顏色後第一次選中它們(並取消選擇)。哪些屬性在第一個item定義中配置不正確?如何使用選擇器設置初始ListView項背景顏色

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="false" android:state_pressed="false" android:drawable="@color/grey" /> 
    <item android:state_pressed="true" android:drawable="@color/red"/> 
</selector> 

還試圖直接設置列表視圖項目的背景,但選擇器不再有任何影響。

回答

3
  • 對於列表項定義了可繪製的選擇器:

RES /繪製/ list_item_background.xml

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

    <item android:state_enabled="true" android:state_pressed="true" android:drawable="@color/red" /> <!-- Pressed state --> 
    <item android:state_enabled="true" android:state_focused="true" android:drawable="@color/black" /> <!-- Focused state --> 
    <item android:state_enabled="true" android:state_selected="true" android:drawable="@color/orange" /> <!-- Selected state --> 
    <item android:drawable="@color/gray" /> <!-- Default state -->  

</selector> 
  • 設置上面繪製作爲背景的個人列表項:

RES /佈局/ list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:background="@drawable/list_item_background" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/listitem" /> 

</LinearLayout> 
  • 使用上面的自定義佈局創建列表項:

    String[] mStrings = {"One", "Two", "Three", "Four"}; ListView list = (ListView) rootView.findViewById(R.id.list); list.setAdapter(new ArrayAdapter<String>(this.getActivity(), R.layout.list_item, R.id.listitem ,mStrings));

+0

在**'item.xml' **上使用屬性'android:background'而不是**'ListView' **元素的屬性'android:listSelector'解決了它,無論出於何種原因。 – Mahoni

1

我建議如下定義嘗試:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@color/red"/> 
    <item android:drawable="@color/grey" /> 
</selector> 

狀態列表可繪製嘗試才能匹配。如果你想在許多情況下使用「default」drawable,它應該是最後一個,沒有狀態標誌。從docs

在每個狀態變化,狀態列表遍歷從上到下 和匹配當前狀態下使用,在 選擇不是基於第一項「最佳匹配」,而只是第一個項目 符合國家的最低標準。

+0

事實上,並沒有試圖改變與一個訂單的是,不幸的是它也不管用...在Android佈局查看器中,它甚至具有正確的顏色 – Mahoni

+0

@Mahoni如何/在哪裏設置背景? – matiash

+0

@matiash直接在佈局中,我使用'ListView'元素的屬性'android:listSelector =「@ drawable/my_listview_selector」' –

1

也試過設置列表視圖項的背景直接,但隨後的選擇沒有任何效果了

如果我沒有弄錯,選擇器被繪製在列表項後面(默認)。由於您將純灰色設置爲列表項目,因此不會顯示選擇器的紅色。您可以通過更改已定義的方式來確認此問題@color/grey - 嘗試向其添加低α值。在這種情況下,紅色應該通過。

您當然可以通過設置android:drawSelectorOnTop="true"來更改此行爲 - 默認爲false。然而,當選擇器被繪製在頂部時,該列表項目將被隱藏。

無論如何 - 從你所說的話,你想要項目有@color/red背景按下時,否則背景@color/grey

看看下面的滿足您的要求:

選擇繪製爲列表項的看法

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@android:color/transparent"/> 
    <item android:drawable="color/grey" /> 
</selector> 

對於listSelector屬性,只提供了顏色值:

<ListView 
    .... 
    .... 
    android:listSelector="@color/red" > 
</ListView> 

簡而言之,當列表項目被按下時,項目背景顏色爲transparent ed - 允許listSelector red color可見。在所有其他情況下,grey顏色是可見的。

+0

這看起來最有前途。列表視圖的'android:listSelector'設置爲'@ color/red',但是我在哪裏設置定義的''呢?在'item.xml'上,如果是,在哪個屬性上? – Mahoni

0

選擇器應該類似於下面的一個:

List_selector。XML

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
    android:state_selected="false" 
     android:state_pressed="false" 
     android:drawable="@drawable/gradient_bg" /> 

    <item android:state_pressed="true" 
     android:drawable="@drawable/gradient_bg_hover" /> 

    <item android:state_selected="true" 
    android:state_pressed="false" 
     android:drawable="@drawable/gradient_bg_hover" /> 
</selector> 

然後在繪項目文件夾,您需要創建以下兩個文件:

gradient_bg.xml

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

    <gradient 
     android:startColor="#cfcfcf" 
     android:centerColor="#737374" 
     android:endColor="##3e3e3e" 
     android:angle="270" /> 
</shape> 

gradient_bg_hover.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#7f0000" 
     android:centerColor="#660000" 
     android:endColor="#4c0000" 
     android:angle="270" /> 
</shape> 

最後,在列表視圖中,您需要添加以下代碼行:

android:background="@drawable/list_selector" 

希望這有助於:)

+0

不,對不起,使用漸變不會改變任何內容。 – Mahoni

+0

大家都在告訴你,你需要在list_selector.xml文件中設置3種狀態: <項目的android:state_selected = 「假」/> <項目的android:state_pressed = 「真」/> <項目的android: state_selected =「true」/> –

+1

是的,我試過了;-)在'item.xml'上使用'background'解決了它不是三種狀態的方法。 – Mahoni

1

你需要在你的繪製文件夾中創建listselector.xml

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

    <item android:state_pressed="true" 
     android:drawable="@drawable/gradient_bg_hover" /> 

    <item android:state_selected="true" 
    android:state_pressed="false" 
     android:drawable="@drawable/gradient_bg_hover" /> 
</selector> 

那麼你的列表視圖中添加:

<ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:divider="#b5b5b5" 
     android:dividerHeight="1dp" 
     android:listSelector="@drawable/listselector" /> 
+0

不起作用,對不起。 – Mahoni

相關問題