12

我正在使用 ABS 版本。4,我需要简单地更改除了动作模式关闭图标之外显示的默认“完成”文本,但我真的不知道该怎么做。

我认为文本需要自定义至少有两个充分的理由:

  • “完成”并不适用于所有上下文(例如“取消”可能更合适,我见过一些应用程序,例如 Galaxy Tab 上的“我的文件”应用程序,使用它)

  • “完成”需要根据用户的语言进行本地化

是否可以自定义该文本?如果是这样,谁能告诉我该怎么做?

提前致谢。

编辑

我找到了一个临时解决方法,我将其发布在以下内容中:

private TextView getActionModeCloseTextView() {
    // ABS 4.0 defines action mode close button text only for "large" layouts
    if ((getResources().getConfiguration().screenLayout & 
        Configuration.SCREENLAYOUT_SIZE_MASK) == 
        Configuration.SCREENLAYOUT_SIZE_LARGE) 
    {
        // retrieves the LinearLayout containing the action mode close button text
        LinearLayout action_mode_close_button =
            (LinearLayout) getActivity().findViewById(R.id.abs__action_mode_close_button);
        // if found, returns its last child 
        // (in ABS 4.0 there is no other way to refer to it, 
        // since it doesn't have an id nor a tag)
        if (action_mode_close_button != null) return (TextView)
            action_mode_close_button.getChildAt(action_mode_close_button.getChildCount() - 1);
    }
    return null;
}

这就是我想出的方法。请注意,它在很大程度上依赖于abs__action_mode_close_item.xmlABS 4.0 的结构。

这适用于我的场景,但是,正如您所看到的,将其提升为真正的“答案”还不够令人满意,这就是为什么我只编辑了我以前的帖子。

希望对其他人有所帮助,但我也希望有人可以分享更好,更清洁的解决方案。

4

6 回答 6

10

您可以使用主题来覆盖默认图标:

    <item name="actionModeCloseDrawable">@drawable/navigation_back</item>
    <item name="android:actionModeCloseDrawable">@drawable/navigation_back</item>
于 2012-07-12T15:12:03.083 回答
7

I edited the code from PacificSky to be able to customize the color and font size of the close button, both in pre ICS and >ICS.

I created a method named customizeActionModeCloseButton

private void customizeActionModeCloseButton() {
      int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");    
      View v = getGSActivity().findViewById(buttonId);
      if (v == null) {
         buttonId = R.id.abs__action_mode_close_button;
         v = getGSActivity().findViewById(buttonId);
      }
      if (v == null)
         return;
      LinearLayout ll = (LinearLayout) v;
      if (ll.getChildCount() > 1 && ll.getChildAt(1) != null) {
         TextView tv = (TextView) ll.getChildAt(1);
         tv.setText(R.string.close_action_mode);
         tv.setTextColor(getResources().getColor(R.color.white));
         tv.setTextSize(18);
      }
   }

and I call it just after calling startActionMode()

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
   actionMode = getActivity().startActionMode(this);
   customizeActionModeCloseButton();
   return true;
}
于 2012-10-31T11:59:44.347 回答
6

已经有一段时间了,但这里有一个稍微不那么老套的解决方案——把它放在那里供后代使用。

对于 Android 版本 < ICS

将以下行放入应用程序的 strings.xml 中:

<string name="abs__action_mode_done">Cancel</string>

这会覆盖 TextView(在 ActionBarSherlock/res/layout-large/abs__action_mode_close_item.xml 中定义)的 android:text 属性。

Android ICS 及以上版本

本机 ActionBar 功能用于 ICS 及更高版本。您需要使用以下代码查找并覆盖与完成按钮关联的字符串:

int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
if (buttonId != 0)
{
    View v = findViewById(buttonId);
    if (v != null)
    {
        LinearLayout ll = (LinearLayout)v;
        View child = ll.getChildAt(1);
        if (child != null)
        {
            TextView tv = (TextView)child;
            tv.setText(R.string.cancel);
        }
    }
}
于 2012-10-23T23:50:00.747 回答
3

感谢太平洋天空的回答。这对我的情况很有用。

这里需要说明的是,在某些情况下findViewById(buttonId)可能会返回null,例如在 onCreateActionMode()函数中调用,因为LinearLayout我猜当时 for ActionMode 关闭按钮尚未初始化。

我想隐藏动作模式关闭按钮,所以我只是sendEmptyMessageDelayedonCreateActionMode() 200 毫秒后打电话给 PacificSky。这个对我有用。

于 2012-11-01T11:51:18.563 回答
0

这是我使用 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);
          }
       }
于 2013-07-23T08:44:40.237 回答
-1

com.actionbarsherlock.view.ActionMode 包含方法:

setTitle

它用于更改操作栏中关闭图标附近的文本。ActionMode 在您的 com.actionbarsherlock.view.ActionMode.Callback 接口实现方法中可用,例如 onCreateActionMode。

您可以做的 - 保存传入的 ActionMode 引用并稍后使用它来更改您喜欢的标题。或者,如果它不是动态的 - 您可以在 onCreateActionMode 中使用常量进行设置。

于 2012-06-15T10:41:24.700 回答