显示自定义布局溢流下拉菜单项?安卓自定义、布局、菜单项

2023-09-12 02:39:48 作者:我不在乎你对我的不在乎。

我试图弄清楚如何以显示溢出菜单内项的自定义视图(在动作条的右侧),但我似乎无法弄清楚。它仍然只显示标题:秒。我想要做的事情,例如Twitter,却有着它显示在下拉菜单中的图标,标题,并在第一项字幕。谁能帮我出

在menu.xml文件档案

 <项目
    机器人:ID =@ + ID / action_dropdown
    机器人:图标=@可绘制/ ic_action_overflow
    机器人:actionProviderClass =efwefwef.com.actionbar.ABDropDown
    机器人:标题=
    机器人:showAsAction =总是>
< /项目>
 

actionProvider类#1

 公共类ABDropDown扩展ActionProvider {
    私有静态最后字符串变量=MActionProvider;
    私人上下文的背景下;

    公共ABDropDown(上下文的背景下){
        超(上下文);
        this.context =背景;
    }

    @覆盖
    公共查看onCreateActionView(){
        返回null;
    }

    @覆盖
    公共布尔hasSubMenu(){
        Log.d(TAG,hasSubMenu);
        返回true;
    }


    @覆盖
    公共布尔onPerformDefaultAction(){
        Log.d(TAG,onPerformDefaultAction);
        返回super.onPerformDefaultAction();
    }

    @覆盖
    公共无效于prepareSubMenu(子菜单的子菜单){
        subMenu.clear();
        subMenu.add(档案)setActionProvider(新ABDropDownProfile(上下文))。
        subMenu.add(Menu.NONE,Menu.NONE,2,魅惑2);

    }

}
 
西门子S7 300模块6ES7312 5BF04 0AB0

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

 公共类ABDropDownProfile扩展ActionProvider {
    私有静态最后字符串变量=MActionProvider;
    私人上下文的背景下;

    公共ABDropDownProfile(上下文的背景下){
        超(上下文);
        this.context =背景;
    }

    @覆盖
    公共查看onCreateActionView(){
        查看查看= View.inflate(背景下,R.layout.actionbar_viewprofile,NULL);

        返回查看;
    }

    @覆盖
    公共布尔hasSubMenu(){
        返回false;
    }


    @覆盖
    公共布尔onPerformDefaultAction(){
        Log.d(TAG,onPerformDefaultAction);
        返回super.onPerformDefaultAction();
    }
}
 

解决方案

我只是面临如何列出自定义行溢出菜单中的同样的问题。不幸的是,没有太多的网络有关自定义动作条。如果要列出过流菜单只有图标和标题的项目,你需要做的项​​目中的项目嵌套。我这样做是这样的:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <项目
        机器人:ID =@ + ID / action_settings
        机器人:图标=@可绘制/ ic_action_overflow
        机器人:orderInCategory =100
        机器人:showAsAction =总是>
        <菜单>
            <项目
                机器人:ID =@ + ID / add_source
                机器人:图标=@可绘制/ ADD_ON
                机器人:orderInCategory =100
                机器人:showAsAction =从不
                机器人:标题=@字符串/ add_source/>
            <项目
                机器人:ID =@ + ID / channel_setup
                机器人:图标=@可绘制/ channelsetup_on
                机器人:orderInCategory =100
                机器人:showAsAction =从不
                机器人:标题=@字符串/ channel_setup/>
        < /菜单>
    < /项目>

    <项目
        机器人:ID =@ + ID /时间
        机器人:orderInCategory =99
        机器人:showAsAction =总是
        机器人:标题=@字符串/ time_title/>
    <项目
        机器人:ID =@ + ID /天气
        机器人:图标=@可绘制/ ic_action_cloud
        机器人:orderInCategory =98
        机器人:showAsAction =总是
        机器人:标题=@字符串/ weather_title/>
    <项目
        机器人:ID =@ + ID /搜索
        机器人:actionViewClass =android.widget.SearchView
        机器人:图标=@可绘制/ ic_action_search
        机器人:orderInCategory =97
        机器人:showAsAction =collapseActionView |总是
        机器人:标题=@字符串/ search_title/>

< /菜单>
 

要实现这件事情:

这里是引用我的问题:

link

我希望它可以帮助你。

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

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 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");

    }

}

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>

to achieve this thing:

And here is the reference to my question:

link

I hope it might help you.