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

Luk*_*uke 12 android actionbarsherlock android-actionbar

我正在使用ABS vers.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;
}
Run Code Online (Sandbox Code Playgroud)

这就是我提出的方法.请注意,它严重依赖于abs__action_mode_close_item.xmlABS 4.0 的结构.

这适用于我的场景,但是,正如你所看到的,将它推广到一个真正的"答案"是不能令人满意的,这就是为什么我只编辑了我以前的帖子.

希望能帮助别人,但我也希望有人可以分享更好,更清洁的解决方案.

Yic*_*ang 10

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

    <item name="actionModeCloseDrawable">@drawable/navigation_back</item>
    <item name="android:actionModeCloseDrawable">@drawable/navigation_back</item>
Run Code Online (Sandbox Code Playgroud)


gui*_*tgl 7

我编辑了PacificSky的代码,以便能够在预ICS和> ICS中自定义关闭按钮的颜色和字体大小.

我创建了一个名为的方法 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);
      }
   }
Run Code Online (Sandbox Code Playgroud)

我打电话后就打电话给他 startActionMode()

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


Pac*_*Sky 6

已经有一段时间了,但是这里的解决方案稍微不那么苛刻 - 把它放在那里供后人使用.

适用于Android版本<ICS

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

<string name="abs__action_mode_done">Cancel</string>
Run Code Online (Sandbox Code Playgroud)

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

适用于Android版本ICS及以上版本

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

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ana*_*liy -1

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

setTitle
Run Code Online (Sandbox Code Playgroud)

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

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