Override context menu colors in Android

前端 未结 3 1842
你的背包
你的背包 2020-11-30 07:53

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

相关标签:
3条回答
  • 2020-11-30 08:31

    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 讨论(0)
  • 2020-11-30 08:55

    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!

    0 讨论(0)
  • 2020-11-30 08:56

    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>
    
    0 讨论(0)
提交回复
热议问题