[英]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.btn
和R.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.