How to change ActionMode background color in Andro

2020-01-27 04:41发布

i am creating an android app in which support level api is 7 so i am using sherlock actionbar. I am using action mode in it. Issue is i want to change the background of action mode. So i have tried

    <item name="android:background">@color/something</item>
    <item name="android:backgroundStacked">@color/something</item>
    <item name="android:backgroundSplit">@color/something</item>

these style solution's available but none of them works. enter image description here

9条回答
【Aperson】
2楼-- · 2020-01-27 05:07

If you are using simple Action Bar then try this,

ActionBar abar = getActionBar();
abar.setBackgroundDrawable(new ColorDrawable(0xff123456));

But if you are using Actionbar share lock Lib then try this

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff123456));

ColorDrawable(0xff123456) - should be your color code.

Drawable/styles.xml

 <item name="android:actionModeBackground">@drawable/actionbar_background</item>
 <item name="actionModeBackground">@drawable/actionbar_background</item>

Change Theme as per API level

If you want to change as per the API level then you should go for this

查看更多
手持菜刀,她持情操
3楼-- · 2020-01-27 05:09

In my case i resolved with this. First i created a style to define the actionModeBackground:

<style name="MyTheme.ActionMode" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionModeBackground">#FFFFFF</item>
</style>

Then i add the style into my base application theme:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionModeStyle">@style/MyTheme.ActionMode</item>
</style>

Hope it helps!!!

查看更多
对你真心纯属浪费
4楼-- · 2020-01-27 05:11

For those who still struggle, and none of the existing solutions helps, please make sure that in your layout with toolbar you have this:

app:popupTheme="@style/Theme.ToolBar"
android:popupTheme="@style/Theme.ToolBar"

and you don't have

app:theme="@style/Theme.ToolBar"

Since they will rise a conflict and your customization will not work.

查看更多
闹够了就滚
5楼-- · 2020-01-27 05:11

The simplest way to do it

ActionBarContextView actionBar = getWindow().getDecorView().findViewById(R.id.action_mode_bar);
    actionBar.setBackgroundColor(WhateverColorYouWant);
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-01-27 05:13

If you want to do it programmatically (supporting AppCompat) you need to look for icon parent view. This code is tested from 2.3.7 to 6.0.1. You need to check on lower API... .

first get your activity decor view

final ViewGroup  decorView = (ViewGroup) getActivity().getWindow().getDecorView();

second set the color in onPrepareActionMode. You can also set the back icon or other elements here

@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) 
{

     decorView.postDelayed(new Runnable() {

     @Override
     public void run() 
     {
        int buttonId = getResources().getIdentifier("action_mode_close_button", "id", "android");

        View v = decorView.findViewById(buttonId);
        if (v == null)
        {
           buttonId = R.id.action_mode_close_button;
            v = decorView.findViewById(buttonId);   
        }

        if (v != null)
        {                         
           ((View)v.getParent()).setBackgroundColor(Color.red /*your color here*/);
        }               
     }   
     }, 500);}
查看更多
一夜七次
7楼-- · 2020-01-27 05:20

Override this style in your custom theme:

<style name="Base.Widget.AppCompat.ActionMode" parent="">
    <item name="background">?attr/actionModeBackground</item>
    <item name="backgroundSplit">?attr/actionModeSplitBackground</item>
    <item name="height">?attr/actionBarSize</item>
    <item name="titleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Title</item>
    <item name="subtitleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle</item>
    <item name="closeItemLayout">@layout/abc_action_mode_close_item_material</item>
</style>

For example:

<style name="LywActionMode" parent="Base.Widget.AppCompat.ActionMode">
    <item name="background">@color/lyw_primary_color</item>
    <item name="backgroundSplit">@color/lyw_primary_color</item>
</style>

<style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light">
    ...
    <item name="actionModeStyle">@style/LywActionMode</item>
</style>
查看更多
登录 后发表回答