简体   繁体   中英

Android Change Hamburger icon position based on ToolBar size

I am trying to set the position of Hamburger icon in ToolBar as Vertically Center, but unable to do so.

I made ToolBar height based on layout weight. I cannot use ToolBar default height because of customer requirement.

在此处输入图片说明

Below is my Main Activity XML:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        app:titleTextColor="@color/vodafone_white"
        android:background="@mipmap/img_appbar">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <android.support.v4.widget.Space
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                 />

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="7"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:textColor="@color/vodafone_white"
                android:textSize="35sp"
                android:textStyle="bold"
                android:gravity="left|center_vertical"
                android:text="@string/title_Vodafone" />

        </LinearLayout>
    </android.support.v7.widget.Toolbar>

    <!--Contents-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8.5"
        android:background="@color/vodafone_lightGray">

        <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:openDrawer="start">

            <!-- Layout for content is here. This can be a RelativeLayout  -->
            <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:id="@+id/app_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.keybs.vodafoneqatar.views.MainActivity">

                <android.support.constraint.ConstraintLayout 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"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    tools:context="com.keybs.vodafoneqatar.views.MainActivity">

                </android.support.constraint.ConstraintLayout>


                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|end"
                    android:layout_margin="@dimen/fab_margin"
                    app:srcCompat="@android:drawable/ic_dialog_email" />
            </android.support.design.widget.CoordinatorLayout>

            <android.support.design.widget.NavigationView
                android:id="@+id/nav_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:fitsSystemWindows="true"
                app:headerLayout="@layout/nav_header_main2"
                app:menu="@menu/activity_main2_drawer" />

        </android.support.v4.widget.DrawerLayout>

    </RelativeLayout>

</LinearLayout>

Is there any way in which I can :

  1. either to make this ActionBarDrawerToggle button vertically centered with some left margin so that the icon will fit in center of white area.

  2. Or to create a custom control as alternate to this ActionBarDrawerToggle button?

Need help as I am new to Android Development.

For vertically alligning the Hamburger Icon

use the minHeight attribute on your toolbar.

Example

<android.support.v7.widget.Toolbar
       android:minHeight="100dp"  //your value

</android.support.v7.widget.Toolbar>

For Accessing Margin | padding Margin | padding In left/right side of toolbar with:

use contentInset

Example:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setContentInsetsAbsolute(toolbar.getContentInsetLeft(), 200); //here 200 is set for padding left

try this

ViewGroup.LayoutParams layoutParams  = mToolbar.getLayoutParams();
for (int i = 0; i < mToolbar.getChildCount(); i++) {
    if (mToolbar.getChildAt(i) instanceof ImageButton) {
        Toolbar.LayoutParams lp = (Toolbar.LayoutParams) mToolbar.getChildAt(i).getLayoutParams();
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM