开发者

Override context menu colors in Android

开发者 https://www.devze.com 2023-02-02 07:47 出处:网络
Let\'s see, i know how to change the style of a ListView (the orange color when an item is selected): android:listSelector=\"@drawable/xxx\" and a dr开发者_JAVA百科awable with a bitmap or a @color

Let's see,

i know how to change the style of a ListView (the orange color when an item is selected):

android:listSelector="@drawable/xxx" and a dr开发者_JAVA百科awable with a bitmap or a @color

<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.


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 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>


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:

<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:

<?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:

<?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:

<?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!


Here is how you do it. Go to resources > styles.xml and override the theme's itemBackgroud attribute value as follows:

<!-- 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:

<application
    ...
    android:theme="@style/AppTheme">

    ...

 </application>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号