ActionBarSherlock(ABS):如何自定义操作模式关闭项目的文本?

12
我正在使用ABS版本4,需要简单更改默认显示在操作模式关闭图标旁边的“完成”文本,但我真的不知道如何做到这一点。
我认为该文本需要定制化至少有两个好处:
1. “完成”并非所有情况下都适用(例如,“取消”可能更合适,我看到一些应用程序,例如Galaxy Tab上的“My Files”应用程序使用它) 2. 根据用户的语言需要本地化“完成”
是否可以自定义该文本?如果可以,请问有谁能告诉我如何实现?
提前致谢。
编辑
我已经找到了一个临时解决方法,我将其发布如下:
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 4.0中abs__action_mode_close_item.xml的结构。
对于我的情况,这个方法可行,但正如你所看到的,它不能被认为足够满意以推广为真正的“答案”,这就是为什么我只编辑了之前的帖子。
希望能帮助其他人,但我也希望有人能分享一个更好、更清洁的解决方案。
6个回答

10

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

    <item name="actionModeCloseDrawable">@drawable/navigation_back</item>
    <item name="android:actionModeCloseDrawable">@drawable/navigation_back</item>

7

我修改了PacificSky的代码,使得能够自定义关闭按钮的颜色和字体大小,在早期的Android版本和4.0以上的版本都能被支持。

我创建了一个名为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);
      }
   }

在调用 startActionMode() 之后,我将其称为。

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
   actionMode = getActivity().startActionMode(this);
   customizeActionModeCloseButton();
   return true;
}

我认为我们应该将“if (v == null)”更改为“if (v == null || buttonId == 0)”在第一个条件中。以避免存在ID = 0的视图。(我曾经遇到过这个问题)^^ - gZerone

6

已经有一段时间了,但这里提供一个稍微不那么hacky的解决方案 - 为后人留下。

对于 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及以上

ICS及以上版本使用原生ActionBar功能。您需要找到并覆盖与完成按钮相关联的字符串,使用以下代码:

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

很好知道,但你可能想考虑一下@gonglong的答案。顺便说一句,我觉得不得不这样做来修复Android API的可用性问题很荒谬。 - mdelolmo

3
感谢PacificSky的回答,对我的情况很有用。
这里需要解释一下的是,在某些情况下(比如在onCreateActionMode()函数中调用),findViewById(buttonId)可能会返回null,因为我猜在那个时候ActionMode关闭按钮的LinearLayout尚未初始化。
我想隐藏操作模式关闭按钮,所以我只需在onCreateActionMode()中发送sendEmptyMessageDelayed,并在200毫秒后调用PacificSky的方法。这对我有效。

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

-1

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

setTitle

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

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


1
setTitle 方法用于设置 ActionMode 的“主”标题,而不是在呈现大型布局时显示在左上角“完成”按钮旁边的文本。 - PacificSky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接