java 覆盖 Android 中的上下文菜单颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4604562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Override context menu colors in Android
提问by ferostar
Let's see,
让我们来看看,
i know how to change the style of a ListView (the orange color when an item is selected):
我知道如何更改 ListView 的样式(选择项目时为橙色):
android:listSelector="@drawable/xxx" and a drawable with a bitmap or a @color
android:listSelector="@drawable/xxx" 和一个带有位图或@color的drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/image" />
<item android:drawable="@android:color/transparent" />
</selector>
The thing is, in order to have a coherent design, i have to do the same thing for a context menu but i just can't see where to change it. There is no listSelector, nothing to change.
问题是,为了有一个连贯的设计,我必须对上下文菜单做同样的事情,但我看不到在哪里改变它。没有 listSelector,没有什么可改变的。
采纳答案by NKijak
If by context menu you mean the menu from the long press, then I have done this with the following code. My menu has my theme's background, and a green highlight.
如果上下文菜单是指长按的菜单,那么我已使用以下代码完成此操作。我的菜单有我的主题背景和绿色突出显示。
context menu layout:
上下文菜单布局:
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/resetConfirm" android:title="@string/actual_reset"></item>
</menu>
styles.xml, where I'm using a custom theme (which I think is the key)
style.xml,我在其中使用自定义主题(我认为这是关键)
<style name="GradientLight" parent="@android:style/Theme.Light">
<item name="android:windowBackground">@drawable/background</item>
<item name="android:progressBarStyle">@style/progressBar</item>
<item name="android:buttonStyle">@style/greenButton</item>
<item name="android:buttonStyleSmall">@style/greenButton</item>
<item name="android:listViewStyle">@style/listView</item>
<item name="android:itemBackground">@drawable/menu_selector</item>
<item name="android:spinnerStyle">@style/spinner</item>
</style>
<style name="listView" parent="@android:style/Widget.ListView.White">
<item name="android:background">@drawable/background</item>
<item name="android:listSelector">@drawable/list_selector_background_green</item>
</style>
回答by user3280064
This is the only approach that worked for me:
这是唯一对我有用的方法:
You can override the android attribute actionModeBackground (which I found in Android/Sdk/platforms/android-22/data/res/values/themes_holo.xml and R.attr) in your app theme:
您可以覆盖应用主题中的 android 属性 actionModeBackground(我在 Android/Sdk/platforms/android-22/data/res/values/themes_holo.xml 和R.attr 中找到):
<style name="AppTheme" parent="android:Theme.Holo">
<item name="android:windowBackground">@color/background</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:actionModeBackground">@drawable/context_menu</item>
...
</style>
and replace it with your own drawable and colors, in this case,
并用您自己的可绘制对象和颜色替换它,在这种情况下,
context_menu.xml:
context_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/context_menu_bottom" />
<item android:drawable="@drawable/context_menu_top"/>
</layer-list>
which is composed of
由
context_menu_bottom.xml:
context_menu_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/accent"/>
<padding android:bottom="4dp"/>
</shape>
and
和
context_menu_top.xml:
context_menu_top.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/primary"/>
</shape>
Hope it helps!
希望能帮助到你!
回答by J. Doe
Here is how you do it. Go to resources > styles.xmland override the theme's itemBackgroudattribute value as follows:
这是你如何做到的。转到资源 >样式.xml并按如下方式覆盖主题的itemBackgroud属性值:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:itemBackground">"YOUR_COLOUR_HERE"</item>
</style>
If this doesn't work, check in AndroidManifet.xml that you are really using the same theme at App level:
如果这不起作用,请检查 AndroidManifet.xml 您确实在应用程序级别使用相同的主题:
<application
...
android:theme="@style/AppTheme">
...
</application>