I asked a similar question here... I got some tutorials in the answers. But this question is diffrenet. because none of that method do not works in my project.
I want center all icons in toolbar
I test all availabe ways ...but always icons are floating in left.
I want centering icons (I prefer left icon floats left and right icon floats right and other icons floats center)
Activity.Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" // I add vertical
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent" // I add match_parent
android:layout_gravity="center" /> // I add center
</LinearLayout>.
Also in tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:background="@color/ColorPrimary"
android:elevation="2dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
android:layout_gravity="center" // I add center
xmlns:android="http://schemas.android.com/apk/res/android" />
in main_menu.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" tools:context=".MainActivity">
<item
android:id="@+id/action_forward"
android:title="forward"
android:icon="@drawable/ic_action_arrow_forward"
app:showAsAction="always"
></item>
<item
android:id="@+id/action_zoom_in"
android:title="zoom in"
android:icon="@drawable/ic_action_zoom_in"
app:showAsAction="always"
></item>
<item ...
and for above code (main_menu.xml) in <item>
I tried all these codes:
android:layout_width="match_parent"
android:layout_weight=".2" // 20%
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
I realy I do not know whats can I do to align icons. I googled hours and I tried all senario
what is my wrong? for all tests I can not see any change ...
before I wanted pic2 (in above picture) But now I just want center it!
For my all test I only get pic 1. without any change.
Toolbar
useRelativeLayout
. – Guarino