How can I customize the Action Mode's color an

2019-01-07 10:49发布

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

8条回答
Fickle 薄情
2楼-- · 2019-01-07 11:05

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.

查看更多
Luminary・发光体
3楼-- · 2019-01-07 11:10

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

查看更多
萌系小妹纸
4楼-- · 2019-01-07 11:12

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.

查看更多
Ridiculous、
5楼-- · 2019-01-07 11:21

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>
查看更多
等我变得足够好
6楼-- · 2019-01-07 11:22

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>
查看更多
疯言疯语
7楼-- · 2019-01-07 11:23

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);
          }
       }
查看更多
登录 后发表回答