【问题标题】:Action mode close button text color动作模式关闭按钮文本颜色
【发布时间】:2021-07-14 10:10:38
【问题描述】:

我看到设置动作模式“完成”/“关闭”按钮的文本颜色。这是我尝试过的:

<item name="android:actionModeCloseButtonStyle">@style/ActionModeCloseButton</item>
....
<style name="ActionModeCloseButton" parent="android:style/Widget.Holo.ActionButton.CloseMode">
    <item name="android:textColor">@android:color/white</item>
</style>

但是没有效果。

请注意,在 JB 上,我将 ActionModeCloseButton 样式的父级设置为常规全息主题就足够了。它在那里工作正常(甚至没有textColor 设置)。

有什么想法吗?

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    首先,文本视图“完成”仅在大型设备上可见。 在 Android 源代码中签出 action_mode_close_item.xml。 所以android:actionModeCloseButtonStyle 只适用于包含视图,而不适用于 imageview 和 textview。

    幸运的是,android 工程师使用可公开访问的属性来设置子视图的样式。

    • 使用android:actionMenuTextColor 更改为TextView 的textColor。
    • 使用android:actionModeCloseDrawable改变ImageView的drawable

    例子:

    <style name="MyTheme">
        <item name="android:actionMenuTextColor">#ff000000</item>
        <item name="android:actionModeCloseDrawable">@drawable/my_close_drawable</item>
    </style>
    

    以下是layout-large 文件夹中action_mode_close_item.xml 的副本,您可以在其中查看布局的构建方式。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/action_mode_close_button"
            android:focusable="true"
            android:clickable="true"
            android:paddingStart="8dip"
            style="?android:attr/actionModeCloseButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="16dip">
        <ImageView android:layout_width="48dip"
                   android:layout_height="wrap_content"
                   android:layout_gravity="center"
                   android:scaleType="center"
                   android:src="?android:attr/actionModeCloseDrawable" />
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:layout_marginStart="4dip"
                  android:layout_marginEnd="16dip"
                  android:textAppearance="?android:attr/textAppearanceSmall"
                  android:textColor="?android:attr/actionMenuTextColor"
                  android:textSize="12sp"
                  android:textAllCaps="true"
                  android:text="@string/action_mode_done" />
    </LinearLayout>
    

    【讨论】:

    • actionMenuTextColor 的问题在于它还会改变“正常”操作栏项目的颜色。
    • 这不起作用,因为要更改的正确项目是 abs__action_mode_close_item.xml 而不是 abs__action_menu_item_layout.xml。问题是abs__action_mode_close_item.xml 没有提供文本颜色:(你可以很容易地为 ABS 修复它,但这仅适用于 4.0 之前的 Android。很抱歉这样说...
    【解决方案2】:

    由于操作模式关闭按钮的布局没有为文本视图提供颜色属性,因此无法在自定义主题中设置此颜色。相反,我发现的唯一方法是覆盖我派生的ActionMode 类的onPrepareActionMode() 方法中的文本颜色:

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... none) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Void none) {
                if (activity != null) {
                    LinearLayout layout = (LinearLayout) activity
                                .findViewById(R.id.abs__action_mode_close_button);
    
                    if (layout == null) {
                        int id = Resources.getSystem().getIdentifier(
                                          "action_mode_close_button", "id", "android");
                        layout = (LinearLayout) activity.findViewById(id);
                    }
    
                    if (layout != null && layout.getChildCount() > 1) {
                        TextView label = (TextView) layout.getChildAt(1);
                        if (label != null) label.setTextColor(Color.RED);
                    }
                }
            }
        }.execute();
    
        return false;
    }
    

    在 Android 4.0 前后的两台设备上工作。

    【讨论】:

    • 小心,如果某个版本的 android 在位置 1 没有 TextView,您的代码将在该设备上爆炸。我会在转换之前对其进行 instanceOf 检查。
    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2017-06-05
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    相关资源
    最近更新 更多