2017-08-27 275 views
1

我尝试在ToolBar中将汉堡包图标的位置设置为垂直居中,但无法这样做。基于ToolBar大小的Android更改汉堡包图标位置

我根据版面重量制作了ToolBar高度。由于客户要求,我无法使用ToolBar默认高度。

enter image description here

下面是我的主要活动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开发新手。

+0

尝试添加'机器人:=了minHeight“100dp”'...在你的'android.support.v7.widget.Toolbar'..then它将在工具栏的中间对齐。 – rafsanahmad007

+0

@ rafsanahmad007感谢您的快速响应。我尝试将最小高度设置为100dp。现在切换按钮显示垂直居中。有什么方法可以为此切换按钮设置左边距? –

回答

1

对于垂直alligning的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 
+0

我已添加答案.... – rafsanahmad007

+0

谢谢,但左填充setContentInsetsAbsolute不显示任何效果。没有填充/保证金添加。 –

+0

它将在Api 21上工作...请参阅:https://stackoverflow.com/questions/29447043/unexplained-gap-padding-to-the-left-between-toolbar-and-linearlayout – rafsanahmad007

0

试试这个

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