How can I customize the Action Mode's color and text?

回眸只為那壹抹淺笑 提交于 2019-12-17 07:19:21

问题


The default action mode (3.0 and up) comes with a green theme and a 'Done' button on the left side. How can I customize these?

Thanks


回答1:


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>



回答2:


Solution for my application

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionModeBackground">@color/bg_action_bar</item>
</style>



回答3:


with this code you can change the Background color of Action mode and also change DONE image. Note: you can add your text in your image too! in res/styles.xml:

<style name="AppTheme" parent="android:Theme.Holo">
<item name="android:actionModeBackground">@android:color/white</item>
<item name="android:actionModeCloseDrawable">@drawable/plus</item>




回答4:


Worked on my project

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="actionModeStyle">@style/CustomActionModeStyle</item>
 </style>

Custom ActionMode style

<style name="CustomActionModeStyle" parent="Base.Widget.AppCompat.ActionMode">
        <item name="background">@color/color_primary</item>
        <item name="titleTextStyle">@style/CustomeActionModeTextStyle</item>
</style>

Custom Title ActionMode

<style name="CustomeActionModeTextStyle" parent="TextAppearance.AppCompat.Widget.ActionMode.Title">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/color_primaryText</item>
</style>



回答5:


You can't really customize it this way because the attribute actionModeStyle is introduced at API level 14. For API levels 11 to 13 you are out of luck.

For API level 14, you can change the style by setting the android:actionModeStyle in your theme.




回答6:


Here is my approach with Java code:

private void customizeActionModeCloseButton(String title, int iconID) {
          int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");    
          View v = findViewById(buttonId);
          if (v == null) {
             buttonId = R.id.abs__action_mode_close_button;
             v = findViewById(buttonId);
          }
          if (v == null)
             return;
          LinearLayout ll = (LinearLayout) v;
          if (ll.getChildCount() > 1 && ll.getChildAt(1) != null) {
             //custom icon
             ImageView img = (ImageView) ll.getChildAt(0);
             img.setImageResource(iconID);
             //custom text
             TextView tv = (TextView) ll.getChildAt(1);
             tv.setText(title);
             tv.setTextColor(Color.WHITE);
          }
       }



回答7:


Updated answer for both pre- and post-Lollipop devices. You must remove the android: prefix to get it to work on Lollipop+ devices, like so:

styles.xml:

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

v21/styles.xml:

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

I'd also recommend having your style with parent="@style/Widget.AppCompat.ActionMode" set, so you inherit the attributes you don't care about overriding.




回答8:


Here's an AppCompat (i.e. using startSupportActionMode) solution for temporarily customizing (customising) the CAB done button's image. Temporarily since it's desirable to change it back to use it's typical image so that when Text Selection kicks in it looks appropriate.

https://gist.github.com/coreform/36ed98f98668f2e90c6a



来源:https://stackoverflow.com/questions/6556116/how-can-i-customize-the-action-modes-color-and-text

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