【问题标题】:Android the navigation toolbar icon and logo can not alignAndroid导航工具栏图标和logo不能对齐
【发布时间】:2017-02-22 03:06:12
【问题描述】:

我设置的工具栏布局 android:layout_gravity="center_vertical" 但它不起作用

代码:

<?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>

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

【问题讨论】:

  • 这里面的imageview是什么?
  • 如果您将这些项目添加到工具栏中的相对或线性布局中会更好。
  • 我在toolbar中删除了我的自定义imageview,以及toolbar.setLogo(),但是结果和上图一样,不能algin左边的drawer icon,为什么?

标签: android toolbar statusbar android-appcompat


【解决方案1】:

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

android:layout_gravity="left|center_vertical"

并确保 paddingmargin0dp

【讨论】:

    【解决方案2】:

    Android 支持 v7 工具栏就像其他视图一样。您可以在工具栏中自定义或添加所需的任何视图。 您的imageView 应该在toolbar 内,并通过gravity or layout_gravity 属性使imageView 与您想要的任何内容对齐。

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

    并将此布局放置在&lt;toolbar&gt; Above layout &lt;/toolbr&gt; 中此代码仅显示一个想法而不是实际代码:)

    【讨论】:

    • 如何设置home_button动画打开抽屉像切换图标动画
    【解决方案3】:

    将这个property属性添加到imageView android:layout_gravity="center_vertical"

    【讨论】:

    • 您可以将文本和图像视图包装在线性布局中并尝试设置其重力
    【解决方案4】:

    这是我的工具栏,中间有一个后退按钮和文本。您可以参考此代码。

            <?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>
    

    如果您在安排最终设置时需要任何帮助,请告诉我。

    【讨论】:

      【解决方案5】:

      如果您尝试自定义 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
      

      【讨论】:

      • 如果我自定义工具栏,如何设置 ImageView 左侧而不是左侧抽屉图标并绑定到drawerToggle。
      • 如果您尝试替换抽屉切换图标,那么它是另一回事。为此,您需要编码。您不需要添加图像视图。过一段时间会更新。
      猜你喜欢
      • 2016-03-06
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多