【问题标题】:Actionbar button is not showing in fragment操作栏按钮未显示在片段中
【发布时间】:2016-08-01 09:57:08
【问题描述】:

我的工具栏按钮没有显示在我尝试使用 setHasOptionsMenu(true); 的片段中。当我单击导航抽屉项目时,它会为每个片段加载新片段,我想显示不同的操作栏按钮(通知或保存)。以下是我的代码,在此先感谢。

以下是我的片段代码:(导航抽屉中的所有片段都相同)

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

}

这是 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>

【问题讨论】:

  • 它是完全没有出现在任何片段中还是仅在某些情况下(没有更新等)?
  • 并非在所有带有导航抽屉的片段中。

标签: android android-fragments android-actionbar android-toolbar


【解决方案1】:

请检查此代码sn-p

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

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多