简体   繁体   English

将3点菜单实现为片段

[英]Implement 3 dot menu to a fragment

Hi guys i want to create 3 dot action bar menu at fragment level, the condition is i want to show that menu at just 1 fragment not on all and if i make them at my main activity, Then i can't hide them so that's why i need to make them at fragment level. 嗨,大家好,我想在片段级别创建3点操作栏菜单,条件是我想只在1个片段上显示该菜单,而不是全部显示,如果我在主要活动中创建它们,那么我就无法隐藏它们,所以为什么我需要使它们处于片段级别。 so, far i have tried this code on my fragment 因此,到目前为止,我已经在片段上尝试了此代码

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    switch (item.getItemId()) {
        case R.id.sync:
            Toast.makeText(this, "Sync data...", Toast.LENGTH_SHORT).show();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

But its saying the method "onCreateOptionsMenu" doesn't override from its super class. 但是它的说法“ onCreateOptionsMenu”没有被其超类覆盖。

It look like I'm miss something very basic, don't know what it is. 看起来我想念一些非常基本的东西,不知道它是什么。

Thanks 谢谢

Try like this 这样尝试

menu_filter.xml menu_filter.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >


    <item
        android:id="@+id/action_filter"
        android:title="@string/filter"
        android:orderInCategory="10"
        android:icon="@drawable/filter"
        app:showAsAction="ifRoom" />


</menu>

OnCreate Method of fragment 片段的OnCreate方法

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

onCreateOptionsMenu onCreateOptionsMenu

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_filter.xml, menu);  // Use filter.xml from step 1
    }

onOptionsItemSelected onOptionsItemSelected

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.action_filter){
            //Do whatever you want to do 
            return true;
        }

        return super.onOptionsItemSelected(item);
    } 

I hope it might help you ! 希望对您有帮助!

Put an ImageButton on fragment's layout with "3 dots" drawable. 使用可绘制的“ 3点”将ImageButton放在片段的布局上。 Then use PopupMenu to show the menu when that ImageButton is clicked. 然后,当单击该ImageButton时,使用PopupMenu显示菜单。 I hope the following answer given by Shylendra helps you: https://stackoverflow.com/a/21329225/7010102 我希望Shylendra提供的以下答案对您有所帮助: https ://stackoverflow.com/a/21329225/7010102

删除override注解,因为它不允许你做so.Or尝试使用不同的XML菜单资源来克服这一problem.Say main_menu是您MainAcitivtymain_frag1您的Fragment

    // Todo Three Dots Code.....
    @Override
    public void onPrepareOptionsMenu(Menu menu) {

    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.minu_filter, menu);
        menu.findItem(R.id.action_enter_manually).setVisible(true);
        menu.findItem(R.id.action_validation_report).setVisible(false);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.action_enter_manually){
            Log.e("keshav","Enter Manually");
            Intent i=new Intent(getActivity(), EnterManually.class);
            startActivity(i);
            //Do whatever you want to do
            return true;
        }
        if(id == R.id.action_validation_report){
            Log.e("keshav","Enter Manually 7128");
            Intent i=new Intent(getActivity(), DateWiseReportActivity.class);
            startActivity(i);

            //Do whatever you want to do
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

----------------------------------------------------------------
                       menu_filter.xml
----------------------------------------------------------------

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <item
        android:id="@+id/action_enter_manually"
        android:icon="@mipmap/enter_manualy_48"
        android:orderInCategory="10"
        android:title="Enter Manually"
        app:showAsAction="collapseActionView" />

    <item
        android:id="@+id/action_validation_report"
        android:icon="@drawable/done"
        android:orderInCategory="10"
        android:title="Validation Report"
        app:showAsAction="collapseActionView" />


</menu>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM