简体   繁体   English

Android:按下popupmenu项时的背景色

[英]Android: background color when popupmenu item is pressed

Android: background color when menu item is pressed Android:按下菜单项时的背景色

I executed the above url, but there is no change. 我执行了上述网址,但没有任何变化。

styles.xml 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 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.... 如下创建一个名为button_pressed.xml的形状。

<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... 假设您有一个拖曳按钮,其id分别为R.id.btnR.id.btn1 ,如下所示...

<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. 如下编写onClick()方法...这将允许您保留更改的颜色,直到按下另一个按钮为止。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Android Selector 更改 PopupMenu 中的项目颜色? - How to use Android Selector to change item color in PopupMenu? android PopupMenu中的可检查项可能无法正常工作 - checkable item in android PopupMenu not working probably Android:按下时按钮不会更改颜色 - Android: Button does not change color when pressed 按下按钮时滑动JButton背景和前景色 - JButton background and foreground color swipe when button pressed Android Java微调项背景颜色 - Android Java Spinner Item Background Color 按下时按钮颜色会发生变化,但是当我进行下一次迭代时,按钮颜色不会变为默认背景颜色 - Button color is changing when pressed but when i go for next iteration that button color is not going to default background color 滚动列表视图时丢失项目背景颜色 - Lost item background color when scrolling listview Android:可扩展列表视图按下项目时按下项目按钮 - Android: Expandable List View Item Buttons Get Pressed When Item Gets Pressed 在Android中按下按钮时如何设置单选按钮的文本颜色? - How to set text color of a radio button when a button is pressed in Android? Android:按下按钮时更改列表视图中所有按钮的颜色 - Android: Change color of all buttons in listview when a button pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM