2017-02-22 61 views
1

the logo cannot align to the left drawer icon 工具栏布局我设置的android:layout_gravity = “center_vertical” ,但它不能正常工作Android的导航工具栏图标和标识不能对齐

代码:

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:background="@drawable/toolbar_bg" 
    android:fitsSystemWindows="true" 
    app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:src="@android:drawable/sym_def_app_icon" /> 

主要布局:

<!-- As the main content view, the view below consumes the entire 
    space available using match_parent in both dimensions. --> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include 
     android:id="@+id/head_tool_bar" 
     layout="@layout/toolbar" /> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/head_tool_bar" /> 

</RelativeLayout> 
<!-- The drawer is given a fixed width in dp and extends the full height of 
    the container. --> 
<fragment 
    android:id="@+id/navigation_drawer" 
    android:name="com.voogolf.helper.home.NavigationDrawerFragment" 
    android:layout_width="@dimen/navigation_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    tools:layout="@layout/drawer_new_home" /> 

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

如何设置抽屉图标和徽标对齐顶部?为什么左侧抽屉图标有填充 这样?

+0

这是什么imageview? –

+0

如果您在工具栏中的相对或线性布局内添加这些项目,它会更好。 –

+0

我在工具栏和toolbar.setLogo()中删除了我的自定义图像视图,但结果与上面的图片相同,无法添加左侧抽屉图标,为什么? –

回答

0

这个属性属性添加到ImageView的android:layout_gravity="center_vertical"

+0

我这样做,但不工作 –

+0

您可以将文本和图像视图包裹在线性布局中,并尝试设置其重力 – Mohamed

0

你应该尝试设置布局重力这样。

android:layout_gravity="left|center_vertical" 

,并确保paddingmargin0dp

0

,如果你想自定义您的toolbar然后做这样的事情。

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.Toolbar 
    ...> 
<RelativeLayout> 
<ImageView 
    ... /> 
//here you can add more view items 
</RelativeLayout> 
</android.support.v7.widget.Toolbar> 

,如果您使用的工具栏和设置使用ActionBarDrawerToggle那么这段代码添加到您的ActionBarDrawerToggle对象操作栏。

toggle.setDrawerIndicatorEnabled(false);  // this will disable your default haburger icon 
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon); // this will replace the toggle home indicator 
+0

如果我自定义工具栏,如何设置ImageView左侧而不是左侧抽屉图标并绑定到drawerToggle。 –

+0

如果您尝试更换抽屉切换图标,那么它是另一回事。为此你需要编码。你不需要添加一个imageview。将在此更新一段时间。 –

0

Android支持v7工具栏与其他视图一样。您可以自定义或添加工具栏内所需的任何视图。 您的imageView应该在toolbar之内,并且使imageViewgravity or layout_gravity属性对齐。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/home_button" 
     android:background="@color/colorToolbarWelcomeBg" 
     android:scaleType="fitCenter" 
     android:src="@drawable/back_arrow" 
     android:layout_gravity="left|centre"/> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.6" 
     android:background="@color/colorToolbarWelcomeBg" 
     android:text="Settings" 
     android:textColor="@color/colorWhite" 
     android:textStyle="bold" 
     android:id="@+id/welcomeToManager" 
     android:gravity="center"/> 
</LinearLayout> 

,并把这种布局内<toolbar> Above layout </toolbr>此代码显示只是一个想法不实际的代码:)

+0

如何设置home_button动画打开抽屉如切换图标动画 –

0

这是我的工具栏在center.You后退按钮和文本可以参考此代码。

 <?xml version="1.0" encoding="utf-8"?> 
     <android.support.v7.widget.Toolbar  xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      android:contentInsetLeft="0dp" 
      android:contentInsetStart="0dp" 

      android:minHeight="?attr/actionBarSize" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:contentInsetLeft="0dp" 
      app:contentInsetStart="0dp" 
      app:contentInsetStartWithNavigation="0dp" 
      app:layout_collapseMode="pin" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:id="@+id/back_arrow" 
        android:layout_width="25dp" 
        android:layout_height="25dp" 
        android:layout_gravity="left|center" 
        android:visibility="visible" 
        android:layout_marginLeft="8dp" 
        android:layout_marginRight="8dp" 
        android:src="@drawable/back_arrow" /> 


       <com.indosat.rider.views.CustomTextView 
        android:id="@+id/toolbar_custom_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:gravity="center" 
        android:text="Register Page" 
        style="@style/roboto_medium" 
        android:textAllCaps="true" 
        android:textColor="@color/white" 
        android:textSize="19sp" /> 



      </FrameLayout> 


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

让我知道如果您需要任何帮助来安排您的最终设置。