【问题标题】:ActionBarSherlock (ABS): how to customize the text of action mode close item?ActionBarSherlock (ABS):如何自定义动作模式关闭项的文本?
【发布时间】:2012-04-05 10:15:05
【问题描述】:

我正在使用 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 4.0 的 abs__action_mode_close_item.xml 的结构。

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

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

【问题讨论】:

    标签: android actionbarsherlock android-actionbar


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我编辑了来自 PacificSky 的代码,以便能够自定义关闭按钮的颜色和字体大小,包括 pre 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);
            }
         }
      

      我在调用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。(我遇到了这个问题)^^
      【解决方案3】:

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

      适用于 Android 版本

      将以下行放入应用程序的 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);
              }
          }
      }
      

      【讨论】:

      • 很高兴知道,但您可能需要考虑@gonglong 的回答。顺便说一句,我觉得为了修复 Android 的 API 可用性错误而不得不这样做很荒谬。
      【解决方案4】:

      感谢 PacificSky 的回答。这对我的情况很有用。

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

      我想隐藏动作模式关闭按钮,所以我只是在onCreateActionMode()sendEmptyMessageDelayed 并在200 毫秒后调用PacificSky。它对我有用。

      【讨论】:

        【解决方案5】:

        这是我使用 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);
                  }
               }
        

        【讨论】:

          【解决方案6】:

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

          setTitle
          

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

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

          【讨论】:

          • setTitle 用于设置 ActionMode 的“主”标题,而不是在渲染大布局时显示在左角“完成”按钮旁边的文本。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-11
          相关资源
          最近更新 更多