在溢出下拉菜单项上显示自定义布局?安卓 [英] Showing Custom Layout on Overflow Drop Down Menu Item? Android

查看:17
本文介绍了在溢出下拉菜单项上显示自定义布局?安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何为溢出菜单(在操作栏的右侧)内的项目显示自定义视图,但我似乎无法弄清楚.它仍然只显示标题:S.我想做一些事情,比如 twitter 如何在下拉菜单的第一项中显示图标、标题和副标题.谁能帮帮我

I am trying to figure out how to show a custom view for items inside the overflow menu (on right of actionbar) but I can't seem to figure it out. It still shows the title only :S. I want to do something like how twitter has it to show an icon, title, and subtitle in the first item in drop down menu. Can anyone help me out

Menu.xml 中的项目

Item in Menu.xml

<item
    android:id="@+id/action_dropdown"
    android:icon="@drawable/ic_action_overflow"
    android:actionProviderClass="efwefwef.com.actionbar.ABDropDown"
    android:title=""
    android:showAsAction="always">
</item>

actionProvider 类 #1

actionProvider Class #1

public class ABDropDown extends ActionProvider  {
    private static final String TAG = "MActionProvider";
    private Context context;

    public ABDropDown(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public View onCreateActionView() {
        return null;
    }

    @Override
    public boolean hasSubMenu() {
        Log.d(TAG, "hasSubMenu");
        return true;
    }


    @Override
    public boolean onPerformDefaultAction() {
        Log.d(TAG, "onPerformDefaultAction");
        return super.onPerformDefaultAction();
    }

    @Override
    public void onPrepareSubMenu(SubMenu subMenu) {
        subMenu.clear();
        subMenu.add("Profile").setActionProvider(new ABDropDownProfile(context));
        subMenu.add(Menu.NONE, Menu.NONE, 2, "Mezz 2");

    }

}

我想显示自定义视图的项目的第二个 ActionProvider 类

Second ActionProvider class for the item I want to show a custom view

public class ABDropDownProfile extends ActionProvider  {
    private static final String TAG = "MActionProvider";
    private Context context;

    public ABDropDownProfile(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public View onCreateActionView() {
        View view = View.inflate(context, R.layout.actionbar_viewprofile, null);

        return view;
    }

    @Override
    public boolean hasSubMenu() {
        return false;
    }


    @Override
    public boolean onPerformDefaultAction() {
        Log.d(TAG, "onPerformDefaultAction");
        return super.onPerformDefaultAction();
    }
}

推荐答案

我刚刚在如何在溢出菜单中列出自定义行时遇到了同样的问题.不幸的是,网上关于自定义操作栏的内容并不多.如果你想在溢流菜单中列出只有图标和标题的项目,你需要在项目中嵌套项目.我是这样做的:

I was just facing the same problem on how to list custom rows in overflow menu. Unfortunately there isn't much on web about customising action bars. If you want to list items with just icon and title in over flow menu, you need to do nesting of items within items. I did it like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_overflow"
        android:orderInCategory="100"
        android:showAsAction="always">
        <menu>
            <item
                android:id="@+id/add_source"
                android:icon="@drawable/add_on"
                android:orderInCategory="100"
                android:showAsAction="never"
                android:title="@string/add_source"/>
            <item
                android:id="@+id/channel_setup"
                android:icon="@drawable/channelsetup_on"
                android:orderInCategory="100"
                android:showAsAction="never"
                android:title="@string/channel_setup"/>
        </menu>
    </item>

    <item
        android:id="@+id/time"
        android:orderInCategory="99"
        android:showAsAction="always"
        android:title="@string/time_title"/>
    <item
        android:id="@+id/weather"
        android:icon="@drawable/ic_action_cloud"
        android:orderInCategory="98"
        android:showAsAction="always"
        android:title="@string/weather_title"/>
    <item
        android:id="@+id/search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/ic_action_search"
        android:orderInCategory="97"
        android:showAsAction="collapseActionView|always"
        android:title="@string/search_title"/>

</menu>

实现这件事:

这里是我的问题的参考:

And here is the reference to my question:

链接

希望能帮到你.

这篇关于在溢出下拉菜单项上显示自定义布局?安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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