Default selector background in Clickable Views

前端 未结 9 1236
执念已碎
执念已碎 2020-12-23 02:15

I have some clickable views and I want to set the default available background that is present on list click (in ICS is a blue color). I have tried putting as background thi

相关标签:
9条回答
  • 2020-12-23 02:24

    There is a way to combine all the valid answers:

    Define a attribute (eg. in values/attrs.xml):

    <attr name="clickableItemBackground" format="reference"/>
    

    In your platform dependent theme section (eg. in values/styles.xml or values/themes.xml) declare this:

    <style name="Theme.Platform" parent="@android:style/Theme.Whatever">
            <item name="clickableItemBackground">@android:drawable/list_selector_background</item>
    </style>
    

    In your platform dependent theme section for api-11+ (eg. in values-v11/styles.xml or values-v11/themes.xml) declare this:

    <style name="Theme.Platform" parent="@android:style/Theme.Holo.Whatever">
            <item name="clickableItemBackground">?android:attr/selectableItemBackground</item>
    </style>
    

    Then use ?attr/clickableItemBackground wherever needed.

    0 讨论(0)
  • 2020-12-23 02:24

    In short - android:background="?selectableItemBackground" ;)

    0 讨论(0)
  • 2020-12-23 02:26

    A solution similar to flx's answer, but without additional attribute definition.

    Platform independent style used for pre-Holo devices (in res\values\styles.xml):

    <style name="SelectableItem">
        <item name="android:background">@android:drawable/list_selector_background</item>
    </style>
    

    Style for Holo devices (API Level 14+) (in res\values-v14\styles.xml):

    <style name="SelectableItem">
        <item name="android:background">?android:attr/selectableItemBackground</item>
    </style>
    

    Apply style to needed view, e.g., LinearLayout:

    <LinearLayout
        style="@style/SelectableItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    ...
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-23 02:28

    It's list_selector_holo_dark or the equivalent holo light version; and these are the defaults in Honeycomb and above. list_selector_background is the non-holo version, used in Gingerbread and below.

    EDIT: I believe (but can't confirm) that the platform agnostic selector is ?android:attr/listSelector

    0 讨论(0)
  • 2020-12-23 02:28

    Alternatively,

    android:background="?android:attr/listChoiceBackgroundIndicator
    

    if you just want the items to highlight blue (or whatever the current default is) while they are clicked.

    0 讨论(0)
  • 2020-12-23 02:28

    Setting Background restricts you from choosing a background color or drawable later! So my advice is to add this style to your styles.xml:

    <style name="TouchableView">
        <item name="android:foreground">?android:attr/selectableItemBackground</item>
    </style>
    

    And then on every view you only need to add:

                android:theme="@style/TouchableView"
    

    Like ANY VIEW!

        <TextView
            android:id="@+id/my_orders"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="0dp"
            android:theme="@style/TouchableView"
            android:background="@color/white"
            android:gravity="left"
            android:padding="10dp"
            android:text="@string/my_orders"
            android:textColor="@color/gray"
            android:textSize="14sp" />
    

    Don't forget to add onClickListener to see the results.

    0 讨论(0)
提交回复
热议问题