簡體   English   中英

Android根據ToolBar大小更改Hamburger圖標的位置

[英]Android Change Hamburger icon position based on ToolBar size

我試圖將“工具欄”中“漢堡包”圖標的位置設置為“垂直居中”,但無法這樣做。

我根據布局權重確定了ToolBar的高度。 由於客戶要求,我無法使用工具欄默認高度。

在此處輸入圖片說明

以下是我的主要活動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>

有什么辦法可以:

  1. 要么使此ActionBarDrawerToggle按鈕垂直居中,並留有一些左邊距,以使圖標適合白色區域的中心。

  2. 還是要創建一個替代此ActionBarDrawerToggle按鈕的自定義控件?

我是Android開發的新手,需要幫助。

用於垂直裝飾Hamburger Icon

使用工具欄上的minHeight屬性。

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

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

訪問Margin | padding Margin | padding在與工具欄的左/右側:

使用contentInset

例:

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

嘗試這個

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM