项目分离器在Android中上下文菜单 [英] Item separator for context menu in Android

查看:78
本文介绍了项目分离器在Android中上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以添加菜单项之间的分隔符在Android的上下文菜单?我没有看到文档中此的任何指示。显然菜单项应该在某些情况下,当他们执行不同类型的业务进行分离。

Is it possible to add a separator between menu items in Android's context menu? I don't see any directions for this in the documentation. Apparently menu items should be separated in some cases when they perform operations of a different kind.

NB。现在的问题是关于上下文菜单,而不是选项菜单。

NB. The question is about context menu, not options menu.

推荐答案

首先我想只有一个变通方法 - 上下文菜单的自定义实现,如的图标上下文菜单为例。这样的code允许菜单项类的自定义视图延伸到一个特定的菜单分离器类。

First I thought of only one workaround - a custom implementation of a context menu, such as Icon Context Menu for example. Such a code allows for extending menu item class to a specific menu separator class with custom view.

<子> ...但一​​段时间后,我发现...

另一个(很容易)解决方案,可以添加菜单项与一排_(下划线)字符(令人惊讶的,这是标准的可以证明的 Android的字体,多个实例的唯一符号顺利并排无间隙),然后使用对齐项文本在Java中code SpannableString

Another (much easier) solution could be adding a menu item with a row of '_' (underline) characters (surprisingly this is the only symbol in standard Android font which multiple instances can be shown smoothly side by side without gaps), and then aligning the item text in Java code using SpannableString.

字符串资源

<string name="hr">______________________________</string>

调整字符串的长度为宜。

Adjust the string length as appropriate.

菜单布局

<group android:checkableBehavior="none" android:enabled="false">
   <item android:id="@+id/menu_gap"
      android:title="@string/hr"
      android:enabled="false" />
</group>

的Java

private void alignCenter(MenuItem item)
{
    SpannableString s = new SpannableString(item.getTitle());
    s.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), 0, s.length(), 0);
    item.setTitle(s);
}

这篇关于项目分离器在Android中上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆