具有自定义标题的android应用的3点设置菜单 [英] 3 dot setting menu for android apps with custom title

查看:410
本文介绍了具有自定义标题的android应用的3点设置菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android应用中, 如果您创建一个新项目,

in an android app, if you create a new project,

然后在需要的手机上自动创建3点设置菜单

then automatically the 3 dot settings menu is created on phones where it is needed

,其处理方式与旧版本中的处理方式相同:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

如果创建自定义标题,则必须自己添加3点菜单

if you create a custom title, you have to add the 3 dot menu yourself

我怎么知道什么时候需要它?是指没有设置按钮的手机

how do I know when it is needed? meaning phones that don't have the settings button

还要如何创建自定义并附加到3点按钮的上下文菜单

also how do i create a context menu that is customized and attached to the 3 dot button

在与Shobhit Puri讨论之后,我了解到我没有考虑使用操作栏,因为我使用的是最低API 8,我没有它,

after a disscusion with Shobhit Puri, I understood that I was not considering the actionbar, since I am using a minimum API 8, I don't have it,

因此,CommonsWare刚刚提供了一个选项来检查设置菜单是否存在(我仍然需要检查API 8中是否存在该菜单)

so there is the option that CommonsWare just supplied to check if the settings menu exists (I still need to check if it exists in API 8)

Shobhit Puri的建议是:

Shobhit Puri's suggestion was:

requestWindowFeature(Window.FEATURE_NO_TITLE); 
setContentView(...) ; 
ActionBar ab = getActionBar(); 
ab.setTitle("My Title"); 
ab.setSubtitle("sub-title") ;

但是核心部分需要API 11或支持库V7 无论哪种方式,由于他的所有帮助,我都无法获得Shobhit Puri的回答,我将在我知道它可行的情况下发布最终解决方案

but that of cores requires API 11 or the support library V7 either way I am excepting Shobhit Puri's answer, because of all his help, and I will post my final solution when I know it works

也感谢CommonsWare的一个很好的答案

also thanks to CommonsWare for a nice answer

我决定暂时使用CommonsWare解决方案,我这样写:

I decided to go with CommonsWare solution for now, I wrote it like this:

if (android.os.Build.VERSION.SDK_INT >= 14) {
            ViewConfiguration vc = ViewConfiguration.get(this);
            if (!vc.hasPermanentMenuKey()) {
                setting_dots.setVisibility(View.VISIBLE);
                setting_dots.setOnClickListener(this);
                registerForContextMenu(setting_dots);
            }
        }

理想情况下,我认为您应该使用操作栏,因为它可以为您提供大部分工作,但是它与API 8存在很多兼容性问题,现在我宁愿避免使用

ideally I think you should use the actionbar, because it provides you with most of the work, but it has a lot of compatability issues with API 8 which for now I would rather avoid

推荐答案

@dumazy指出,操作栏的菜单溢出"图标仅显示在没有硬件菜单按钮的设备上.

As @dumazy pointed out that the Action bar's Menu Overflow icon is only shown on those devices which do not have a hardware menu-button.

我怎么知道什么时候需要它?表示没有设置按钮的手机

How do I know when it is needed? meaning phones that don't have the settings button

这由Android本身处理.不用担心

This is handled by Android itself. You don't need to worry.

我如何创建自定义并附加到3点按钮的上下文菜单

how do i create a context menu that is customized and attached to the 3 dot button

您只能在resMenu文件夹中包含一个xml文件.然后,您可以在MenuInflater中指定xml文件.例如:

You can just have a an xml file inside Menu folder in res. Then you can specify the xml file inside the MenuInflater. Eg:

将其命名为list_menu.xml

?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/menu_item_1"
        android:title="@string/menu_string_1" 
        android:showAsAction="ifRoom|withText"/>
    <item android:id="@+id/menu_item_1"
        android:title="@string/menu_string_2" 
        android:showAsAction="ifRoom|withText"/>
</menu>

onCreateOptionsMenu中,您可以将其设置为:

In the onCreateOptionsMenu you can set it as:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.list_menu, menu);
    return true;
}

此菜单将附加到溢出图标,并包含您要在单击时显示的项目.有一些黑客,例如可以在所有设备上显示溢出图标,但强烈建议不要使用它们.让android自行处理.

This menu would be attached to the overflow-icon and have the items that you want to show when it is clicked. There are some hacks like this which can show the overflow-icon on all devices but using them is highly discouraged. Let android handle this itself.

您似乎在使用标题栏.而是尝试使用操作栏.

You seem to use Title bar. Instead, try to use Action Bar for the same.

希望这能回答您的问题.

Hope this answers your question.

这篇关于具有自定义标题的android应用的3点设置菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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