我正在使用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;
}
这就是我提出的方法。请注意,它严重依赖ABS 4.0 abs__action_mode_close_item.xml
的结构。
这适用于我的场景,但是,正如您所看到的,将它推广到真正的“答案”并不足以令人满意,这就是为什么我只编辑了我以前的帖子。
希望能够帮助别人,但我也希望有人可以分享更好更清洁的解决方案。
答案 0 :(得分:10)
您可以使用主题覆盖默认图标:
<item name="actionModeCloseDrawable">@drawable/navigation_back</item>
<item name="android:actionModeCloseDrawable">@drawable/navigation_back</item>
答案 1 :(得分:7)
我编辑了PacificSky的代码,以便能够在pre ICS和&gt; 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;
}
答案 2 :(得分:6)
已经有一段时间了,但这里的解决方案稍微不那么苛刻 - 把它放在后面为后人做。
适用于Android版本&lt; 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);
}
}
}
答案 3 :(得分:3)
感谢PacificSky的回答。这对我的情况很有用。
这里需要解释的是findViewById(buttonId)
在某些情况下可能会返回null
,例如在o nCreateActionMode()
函数中调用,因为ActionMode关闭按钮的LinearLayout
不是但我猜想那时已经初始化了。
我想要隐藏动作模式关闭按钮,所以我只需sendEmptyMessageDelayed
中的onCreateActionMode()
,然后再打电话给PacificSky 200ms。它对我有用。
答案 4 :(得分: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);
}
}
答案 5 :(得分:-1)
com.actionbarsherlock.view.ActionMode包含方法:
setTitle
用于更改ActionBar中“关闭”图标附近的文本。 ActionMode可在com.actionbarsherlock.view.ActionMode.Callback接口实现方法中使用,例如onCreateActionMode。
你可以做什么 - 保存传入的ActionMode引用,并在以后使用它来更改标题。或者,如果它不是动态的 - 您可以使用onCreateActionMode中的常量设置。