2

背景が設定されたListViewアイテムがあります。これは、アイテムが押された/選択されたときに表示されるデフォルトの青いハイライトを上書きします。バックグラウンドとセレクターの両方を持つ方法はありますか?

これは、背景とセレクターの両方をマージしようとする私の試みです...

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

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

    <item android:state_pressed="true" android:color="@color/red"/>

    </selector>
    <item>
        <shape
            android:dither="true"
            android:shape="rectangle" >

            <solid android:color="#ccc" />
        </shape>
    </item>
    <item android:bottom="2dp">
        <shape
            android:dither="true"
            android:shape="rectangle" >
            <corners android:radius="6dp" />

            <solid android:color="@android:color/white" />

             </shape>
    </item>

</layer-list>

これは私のdrawableフォルダーにあり、ListItem xml でこれを設定します。

android:background="@drawable/my_background
4

3 に答える 3

2

カスタムの背景とデフォルトのセレクター効果 (押された/選択されたときに別の drawalbe) を持つのは少し難しいですが、数回試行した後、作成しました。

別々の xml ファイルで 2 つのセレクターを定義する必要があります:listitem_background.xmllistitem_selector.xml.

最初のものはリスト項目の背景に使用され、項目が押されて通常の状態にあるときに効果を発揮します。

2 つ目はリストのセレクターに使用され、すべての状態を に設定することで、リスト ビューのデフォルトのセレクターを取り除きますtransparent

デフォルトのセレクター効果は、最初の xml ファイル listitem_background.xml で定義されています。

color_drawable.xmlまず、いくつかの描画可能な色を定義するための xml ファイルが必要です:res/valuesディレクトリ:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- The color of the normal state. -->
    <drawable name="listitem_normal">#E671B8</drawable>
    <!-- The two color below show when the item is pressed, you should change that to the color you want. -->
    <drawable name="listitem_pressed">#e7eeab</drawable>
    <drawable name="listitem_selected">#e7eeab</drawable>

</resources>

次に、listitem_background.xmlres/drawable

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

    <item android:drawable="@drawable/listitem_pressed" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/listitem_selected" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@drawable/listitem_selected" android:state_enabled="true" android:state_selected="true"/>
    <item android:drawable="@drawable/listitem_normal"/>

</selector>

そして、listitem_selector.xmlres/drawable

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

    <item android:drawable="@color/android:transparent" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@color/android:transparent" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@color/android:transparent"/>

</selector>

listitem_background を listitem に設定します。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/listitem_background" >

    ...

</RelativeLayout>

listitem_selector をリストビューに設定します。

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:listSelector="@drawable/listitem_selector" />
于 2013-08-27T18:43:06.593 に答える
0

これを ListView で使用して、色の組み合わせを一致させます

android:cacheColorHint="#e7eeab"

于 2013-08-21T07:54:57.220 に答える