How to change ActionMode background color in Android

不羁岁月 提交于 2019-11-26 11:14:10

问题


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.


回答1:


if you want change the color of ActionBar just do this:

 ActionBar bar = getActionBar();
 bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

see the following link for more info

and if you using ActionMode This is the style used for any ActionMode. You'll need to create your own style to customize it

<style name="Widget.ActionMode">
<item name="android:background">?android:attr/actionModeBackground</item>
<item name="android:backgroundSplit">?android:attr/actionModeSplitBackground</item>
<item name="android:height">?android:attr/actionBarSize</item>
<item name="android:titleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Title</item>
<item name="android:subtitleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Subtitle</item>
</style>

more info in this site

see this too

Edit

for pre-Honeycomb see this please

maybe this or this helped you




回答2:


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




回答3:


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




回答4:


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);}



回答5:


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>



回答6:


This is the style used for any ActionMode, I pulled it from the SDK. You'll need to create your own style to customize it. It's really easy to do. If you've never done anything like this before, you should read through this post on customizing the ActionBar. It explains everything you'll need to know.

<style name="Widget.ActionMode">
<item name="android:background">?android:attr/actionModeBackground</item>
<item name="android:backgroundSplit">?android:attr/actionModeSplitBackground</item>
<item name="android:height">?android:attr/actionBarSize</item>
<item name="android:titleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Title</item>
<item name="android:subtitleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Subtitle</item>
</style>



回答7:


The simplest way to do it

ActionBarContextView actionBar = getWindow().getDecorView().findViewById(R.id.action_mode_bar);
    actionBar.setBackgroundColor(WhateverColorYouWant);



回答8:


I wanna make more clearer this answer. First create custom background "storage_list_header_shape.xml" in your drawable folder.

<?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle" >

   <gradient
      android:angle="270"
      android:startColor="@android:color/holo_orange_light"
      android:centerColor="@android:color/holo_orange_dark"
      android:endColor="@android:color/holo_orange_light" />

   <size android:height="3dp" />

 </shape>

Second, create custom style in your style.xml

<style name="customActionModeTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionModeBackground">@drawable/storage_list_header_shape</item>
</style>

Third, call the style in your manifest.

<activity
     android:name="com.javacafe.activities.Main"
     android:launchMode="singleTop"
     android:screenOrientation="sensorLandscape"
     android:theme="@style/customActionModeTheme" >
</activity>



回答9:


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.



来源:https://stackoverflow.com/questions/20769315/how-to-change-actionmode-background-color-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!