簡體   English   中英

Android:按下popupmenu項時的背景色

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

Android:按下菜單項時的背景色

我執行了上述網址,但沒有任何變化。

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>

爪哇

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

如下創建一個名為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>

假設您有一個拖曳按鈕,其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>

如下編寫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;
    }
}

希望這項工作對您有用。請讓我知道它是否有效。

在下面實現。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM