简体   繁体   English

动作栏按钮未显示在片段中

[英]Actionbar button is not showing in fragment

My toolbar button is not showing in fragments i tried with setHasOptionsMenu(true); 我使用setHasOptionsMenu(true)尝试的片段中没有显示我的工具栏按钮 . when i click on navigation drawer item it loads new fragment for the each fragment i want to show different action bar button(notification or save). 当我单击导航抽屉项目时,它将为每个片段加载新片段,我想显示不同的操作栏按钮(通知或保存)。 Following is my code,thanks in advance. 以下是我的代码,谢谢。

Following is my Fragment code: (It is same for all fragments in navigation drawer) 以下是我的片段代码:(对于导航抽屉中的所有片段都是相同的)

public class Company1 extends Fragment {

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

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_company_profile,container, false);

    init(view);

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showAddDialog();
        }
    });


    return view;
}

 @Override
 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.company_profile, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.notification:
            break;

     }
     return super.onOptionsItemSelected(item);
 }

}

This is company_profile.xml 这是company_profile.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/notification"
        android:icon="@drawable/ic_action_notification"
        android:title="@string/mnuNotificationText"
        app:showAsAction="always"/>
  </menu>

Please check this code snippet 请检查此代码段

public class Company1 extends Fragment {

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

  }

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_company_profile,container, false);
    setHasOptionsMenu(true);
    init(view);

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showAddDialog();
        }
    });


    return view;
}

 @Override
 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.company_profile, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.notification:
            break;

     }
     return super.onOptionsItemSelected(item);
 }
 @Override
    public void onPrepareOptionsMenu(Menu menu) {
       //You can change menuitem property
       //menu.findItem(R.id.notification).setVisible(true);
        super.onPrepareOptionsMenu(menu);

    }
}

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

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