简体   繁体   中英

Android: background color when popupmenu item is pressed

Android: background color when menu item is pressed

I executed the above url, but there is no change.

styles.xml


    <style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
        <item name="android:textColor">@color/font_color</item>
        <item name="android:colorBackground">@color/dialog_background_color</item>
        <item name="android:dropDownSelector">@drawable/listselector_popup</item>
        <item name="android:listViewStyle">@style/CustomListView2</item>
    </style>
    <style name="CustomListView2" parent="@android:style/Widget.ListView">
        <item name="android:listSelector">@drawable/listselector_popup</item>
    </style>

listselector_popup.xml

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

java

        Context wrapper = new ContextThemeWrapper(context, R.style.PopupMenu);
        PopupMenu popup = new PopupMenu(wrapper, v);

Create a shape named button_pressed.xml as follows....

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

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

    <stroke
        android:width="4dp"
        android:color="@color/blue" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

</shape>

Suppose, you have tow button whose id s are R.id.btn and R.id.btn1 as follows...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        android:background="@drawable/button_pressed"
        android:onClick="onClick"
        android:text="Press Me 1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        android:background="@drawable/button_pressed"
        android:onClick="onClick"
        android:text="Press Me 2" />

</LinearLayout>

Write onClick() method as follows...which will allow you to retain the changed color until another button pressed.

Button button;

public void onClick(View v) {

    Drawable dr = getResources().getDrawable(R.drawable.button_pressed);
    dr.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP);

    switch (v.getId()) {
    case R.id.btn:

        if (button == null) {
            button = (Button) findViewById(v.getId());
        } else {
            button.setBackgroundResource(R.drawable.button_pressed);
            button = (Button) findViewById(v.getId());
        }
        button.setBackgroundDrawable(dr);

        break;

    case R.id.btn2:
        if (button == null) {
            button = (Button) findViewById(v.getId());
        } else {
            button.setBackgroundResource(R.drawable.button_pressed);
            button = (Button) findViewById(v.getId());
        }
        button.setBackgroundDrawable(dr);

        break;

    default:
        break;
    }
}

Hope this work for you.Just let me know if it work.

Realized in the following.

styles.xml
    <style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
        <item name="android:textColor">@color/font_color</item>
        <item name="android:colorBackground">@color/dialog_background_color</item>
        <item name="android:itemBackground">@drawable/listselector_popup</item>
    </style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM