【问题标题】:Android Change Hamburger icon position based on ToolBar sizeAndroid根据工具栏大小更改汉堡图标位置
【发布时间】:2017-08-29 17:04:24
【问题描述】:

我正在尝试将工具栏中汉堡图标的位置设置为垂直居中,但无法这样做。

我根据布局重量制作了工具栏高度。由于客户要求,我无法使用 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 开发新手。

【问题讨论】:

  • 尝试将android:minHeight="100dp"...添加到您的android.support.v7.widget.Toolbar..然后它将与工具栏的中间对齐。
  • @rafsanahmad007 感谢您的快速回复。我尝试将最小高度设置为 100dp。现在切换按钮显示垂直居中。有什么办法可以为这个切换按钮设置左边距?

标签: android hamburger-menu actionbardrawertoggle


【解决方案1】:

用于垂直对齐Hamburger Icon

使用工具栏上的minHeight 属性。

示例

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

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

用于访问Margin | padding 在工具栏的左侧/右侧:

使用contentInset

示例:

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

【讨论】:

  • 谢谢,但左填充 setContentInsetsAbsolute 没有显示任何效果。没有添加填充/边距。
  • 它可以在 Api 21 上运行...见这个:stackoverflow.com/questions/29447043/…
  • 您也可以使用:setPadding() 例如:toolbar.setPadding(200, toolbar.getPaddingTop(), 200 , toolbar.getPaddingBottom());..
  • 我在这里尝试为整个工具栏执行 setPadding() 。是否有可能我们只能为汉堡图标设置填充?
【解决方案2】:

试试这个

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

【讨论】:

    猜你喜欢
    • 2015-03-24
    • 2017-07-12
    • 1970-01-01
    • 2015-03-20
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多