简体   繁体   English

Android studio中fragment实现菜单

[英]Implement menu in fragment in Android studio

I want to implement menu in fragment class. The fragment works fine and there is not error but there is no menu shown in the fragment.我想在片段 class 中实现菜单。片段工作正常,没有错误,但片段中没有显示菜单。 I am implementing the class in the following way我正在通过以下方式实施 class

public class FilesFragment extends Fragment {

    public FilesFragment() {
        // Required empty public constructor
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_files, container, false);

        return view;
    }

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

    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.files_menu,menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
     @Override
     public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_setting){
        Toast.makeText(getActivity(),"setting clicked",Toast.LENGTH_SHORT).show();
    }
    return super.onOptionsItemSelected(item);
}

}

and this is the xml file of files_menu in menu folder.这是菜单文件夹中 files_menu 的 xml 文件。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_setting"
        android:title="settings"
        app:showAsAction="never"/>
    <item
        android:id="@+id/aobut"
        android:title="About"
        app:showAsAction="never"
        />

</menu>

The above code is not showing the menu at all.上面的代码根本没有显示菜单。 How do menu is implemented in the fragment?菜单如何在片段中实现?

在此处输入图像描述

You can populate the menu in any view without any complexity.您可以在任何视图中填充菜单,没有任何复杂性。 Use the following code and enjoy.使用以下代码并享受。

any View can be a button, imageView or any other else in which you want to inflate this menu.任何视图都可以是按钮、imageView 或您想要在其中展开此菜单的任何其他视图。 No need to add menu in xml. Just do in java无需在xml中添加菜单,在java中添加即可

    PopupMenu popup = new PopupMenu(context, any View);
    popup.inflate(R.menu.files_menu );
    popup.setOnMenuItemClickListener(item -> {


        int id = item.getItemId();

        if (id == R.id.action_setting) {
            // do stuff
        } else if (id == R.id.aobut) {
            //

        } 


        return true;
    });
    popup.show();

I want to implement menu in fragment class.我想在片段 class 中实现菜单。 The fragment works fine and there is not error but there is no menu shown in the fragment.该片段工作正常,没有错误,但片段中没有显示菜单。 I am implementing the class in the following way我正在通过以下方式实现 class

public class FilesFragment extends Fragment {

    public FilesFragment() {
        // Required empty public constructor
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_files, container, false);

        return view;
    }

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

    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.files_menu,menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
     @Override
     public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_setting){
        Toast.makeText(getActivity(),"setting clicked",Toast.LENGTH_SHORT).show();
    }
    return super.onOptionsItemSelected(item);
}

}

and this is the xml file of files_menu in menu folder.这是菜单文件夹中files_menu的xml文件。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_setting"
        android:title="settings"
        app:showAsAction="never"/>
    <item
        android:id="@+id/aobut"
        android:title="About"
        app:showAsAction="never"
        />

</menu>

The above code is not showing the menu at all.上面的代码根本没有显示菜单。 How do menu is implemented in the fragment?菜单是如何在片段中实现的?

在此处输入图像描述

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

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