问题
I am stuck simple issue and i know i am doing something minor wrong , My issue is when i am using set Toolbar in MainActivity and after when i am trying to add optionMenu in fragment at a time my toolBar's child component(RelativeLayout in my case) size changed..
Here is my code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/admin_toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/button_blue"
app:contentInsetStart="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimaryDark">
<TextView
android:id="@+id/admin_toolbar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@string/zenith"
android:textColor="@color/white"
android:visibility="gone" />
<ImageView
android:id="@+id/admin_toolbar_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:src="@drawable/ic_drawer_icon" />
<ImageView
android:id="@+id/adminhome_cust_titleimage"
android:layout_width="@dimen/_100sdp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter"
android:src="@drawable/small_app_logo" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<include layout="@layout/content_admin_main" />
</LinearLayout>
Fragment :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_admin_home, container, false);
utils = new Utils(getActivity());
calendarView = (CompactCalendarView) view.findViewById(R.id.admin_compactcalendar_view);
setHasOptionsMenu(true);
toolbar = (Toolbar) getActivity().findViewById(R.id.admin_toolbar);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.adminhome_profilelogo, menu);
}
How can i set my imageView in proper center of toolas you can see in first image..
回答1:
Try this
<ImageView
android:id="@+id/admin_toolbar_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:src="@drawable/ic_drawer_icon" />
<ImageView
android:id="@+id/adminhome_cust_titleimage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/admin_toolbar_icon"
android:layout_alignParentRight="true"
android:scaleType="fitCenter"
android:src="@drawable/small_app_logo" />
But anyway if you will add more menu elements your icon will be centered from end of home button and start of menu. Also you can remove setHasOptionsMenu
and add one more ImageView
in your RelativeLayout
and your code will works.
回答2:
Instead of using option menu
you can add an extra ImageView
to Toolbar RelativeLayout
with aligned right for your profile icon.
Here is an example:
<android.support.v7.widget.Toolbar
android:id="@+id/admin_toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/colorPrimaryDark"
app:contentInsetStart="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">
<ImageView
android:id="@+id/admin_toolbar_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:src="@drawable/ic_drawer_icon" />
<ImageView
android:id="@+id/adminhome_cust_titleimage"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
来源:https://stackoverflow.com/questions/43735960/custom-component-toolbar-fragment-with-onoptionmenu-items