I am using the design support library`s CollapsingToolbarLayout.My issue is i am not able to inflate the menu action icon in toolbar. I am using the below code to create CollapsingToolbarLayout.
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="240dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="32dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/profile_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<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/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
In Fragment i use
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_emp_details, menu);
super.onCreateOptionsMenu(menu,inflater);
}
and setHasOptionsMenu(true);
There are two ways to do it:
First you need to tell the Activity
that the Toolbar
your using is Action Bar
by using: setSupportActionBar(toolbar)
Else (I recommend):
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.your_menu_items);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
TODO: Write your logic here
}
});
I believe when you say "Action icon in toolbar" you mean the UP
action on the left-hand side of the toolbar.
If you mean the right-hand side menu with the 3-dots icon, @Janhavi answer is correct. If you mean the UP
icon on the left-hand side, read below:
You just need to configure it on the toolbar after you inflate the layout, like the following code:
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate your view normally
View root = inflater.inflate(R.layout.my_layout, container, false);
// configure your views
Toolbar toolbar = (Toolbar)root.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.icon);
toolbar.setNavigationOnClickListener(new View.OnClickListener(){
@Override public void onClick(View v){
// TODO: code your UP navigation here
// probably: getFragmentManager().popBackStack ?
}
});
// return the view
return root;
}
the icon you can download from here: https://www.google.com/design/icons/
来源:https://stackoverflow.com/questions/31746644/inflate-menu-in-collapsingtoolbarlayout-issue