因此,我創建menu_event.xml定製我的片段,像這樣的工具欄佈局:動態改變工具欄佈局
<item android:id="@+id/action_add"
android:icon="@drawable/ic_action_new"
android:title="@string/action_settings"
android:showAsAction="ifRoom"
tools:ignore="AppCompatResource" />
<item android:id="@+id/action_search"
android:icon="@drawable/search_icon"
android:title="@string/action_search"
android:showAsAction="ifRoom"
tools:ignore="AppCompatResource" />
爲了做到這一點,我用了onCreateOptionsMenu()方法,我也在我的onCreateView()方法中設置了setHasOptionsMenu(true):
public class ListEventActivity extends Fragment {
public ListEventActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.activity_list_event, container, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_event, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
但即使這樣,它不會改變工具欄佈局和動作溢出項目(相反,它增加了「設置」和「搜索」到它的項目...
任何關於如何解決它的想法?
我使用它,但它不會將圖標添加到我的操作欄,它只會將項目添加到溢出菜單。 – Chris