我试图将菜单分配到CollapsingToolbarLayout内的工具栏。这些是xml和Java代码。
问题是inflateMenu
什么都不做。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="180dp"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/groupPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_collapseMode="pin"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
爪哇:
@InjectView(R.id.toolbar)
Toolbar toolbar;
@InjectView(R.id.toolbar_layout)
CollapsingToolbarLayout toolbarLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_details);
ButterKnife.inject(this);
setSupportActionBar(toolbar);
toolbarLayout.setTitle(conversation.getTargetName());
toolbar.setNavigationIcon(R.drawable.ic_keyboard_backspace_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
toolbar.inflateMenu(R.menu.menu_conversation_activity);
...
}
这是菜单:
<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_call"
android:icon="@drawable/ic_action_phone_24dp"
android:title="@string/secure_notes"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/secureNote"
android:icon="@drawable/ic_pin_white_24dp"
android:title="@string/secure_notes"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/deleteConversation"
android:icon="@drawable/ic_settings_white_24dp"
android:title="@string/delete_conversation"
app:showAsAction="never"/>
<item
android:id="@+id/clearHistory"
android:icon="@drawable/ic_settings_white_24dp"
android:title="@string/clear_history"
app:showAsAction="never"/>
<item
android:id="@+id/changeBackground"
android:icon="@drawable/ic_settings_white_24dp"
android:title="@string/change_background"
app:showAsAction="never"/>
</menu>
答案 0 :(得分:1)
尝试覆盖 onCreateOptionsMenu()方法。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_conversation_activity, menu);
return true;
}
希望这是有帮助的