如何自定义操作模式的颜色和文本?

发布于 2024-11-18 15:59:04 字数 60 浏览 1 评论 0原文

默认操作模式(3.0 及更高版本)带有绿色主题和左侧的“完成”按钮。我如何定制这些?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

那支青花 2024-11-25 15:59:04

这是用于任何 ActionMode 的样式,我从 SDK 中提取了它。您需要创建自己的样式来自定义它。这真的很容易做到。如果您以前从未做过类似的事情,您应该通读 这篇文章介绍了自定义 ActionBar。它解释了您需要了解的一切。

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

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>
歌枕肩 2024-11-25 15:59:04

我的应用程序的解决方案

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

Solution for my application

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionModeBackground">@color/bg_action_bar</item>
</style>
蹲在坟头点根烟 2024-11-25 15:59:04

在我的项目中工作

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

自定义 ActionMode 样式

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

自定义标题 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>

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>
谈下烟灰 2024-11-25 15:59:04

使用此代码,您可以更改操作模式的背景颜色,还可以更改完成图像。注意:您也可以在图像中添加文字!在 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>

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>

假面具 2024-11-25 15:59:04

您实际上无法通过这种方式自定义它,因为属性 actionModeStyle 是在 API 级别 14 中引入的。对于 API 级别 11 到 13,您就不走运了。

对于 API 级别 14,您可以通过在主题中设置 android:actionModeStyle 来更改样式。

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.

南汐寒笙箫 2024-11-25 15:59:04

这是我使用 Java 代码的方法:

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

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);
          }
       }
梦魇绽荼蘼 2024-11-25 15:59:04

更新了棒棒糖前和棒棒糖后设备的答案。您必须删除 android: 前缀才能使其在 Lollipop+ 设备上工作,如下所示:

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>

我还建议您使用 parent="@style/Widget.AppCompat 的样式.ActionMode" 设置,这样您就可以继承您不关心覆盖的属性。

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.

桃扇骨 2024-11-25 15:59:04

这是一个 AppCompat(即使用 startSupportActionMode)解决方案,用于临时自定义(自定义)CAB 完成按钮的图像。暂时因为需要将其更改回使用其典型图像,以便当文本选择启动时它看起来合适。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文