【问题标题】:Add image to center of toolbar将图像添加到工具栏的中心
【发布时间】:2017-12-20 18:29:18
【问题描述】:

我在我的应用程序中使用工具栏而不是 actionabar。工具栏工作正常,但问题是我希望应用程序徽标出现在工具栏的中心。如果没有菜单项显示,图像将在中心工具栏,但如果我将搜索或刷新菜单项添加到工具栏,图像也会发生变化。我希望工具栏始终位于中心

代码

<android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_home"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/pink"
                android:minHeight="56dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:contentInsetEnd="0dp"
                app:contentInsetStart="0dp">

                <RelativeLayout
                    android:id="@+id/rl_toolbar_main"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    >


                    <ImageView
                        android:id="@+id/tv_home_header"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:src="@drawable/image"
                        android:drawablePadding="10dp"
                         />


                </RelativeLayout>

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

【问题讨论】:

  • 只需在相对布局中手动添加您的项目。无需使用默认按钮。它非常简单,您可以使用工具栏视图做任何事情。
  • 但我需要溢出弹出窗口
  • @ArchanaVetkar 如果需要,您可以制作自己的溢出菜单。这只是一个简单的dialog 包含一个ListView。老实说,你可能会做得更好。
  • 是的,您可以像这样制作自定义视图。只需要一点点搜索。

标签: android toolbar


【解决方案1】:

使用具有中心重心的可绘制位图作为工具栏背景可帮助您创建所需的效果。例如,

假设您要居中的图片是 centeree.png,它将被包裹在 drawable/toolbar_background.xml

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@android:drawable/centeree"
    android:gravity="center" />

您可以将 toolbar_background.xml 指定为布局中工具栏的背景

<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@drawable/toolbar_background">

作为替代方案,您可以使用 9 个可绘制的补丁,两侧具有相同的可拉伸区域。

【讨论】:

  • 我必须将 "android:src="@android:drawable/centeree" 更改为 android:src="@drawable/centeree" 才能完成这项工作。聪明的解决方案,谢谢。
【解决方案2】:

你不能这样做是因为

一个或多个自定义视图。应用程序可以将任意子视图添加到工具栏。它们将出现在布局中的这个位置。如果子视图的 Toolbar.LayoutParams 指示重力值为 CENTER_HORIZONTAL,则在测量所有其他元素后,视图将尝试在工具栏中剩余的可用空间内居中。

【讨论】:

    【解决方案3】:

    一种简单的方法,尽管它可能不是正确的方法。

    我将右侧内边距设置为与汉堡图标相同的宽度。然后是我在dimens.xml 中想要的所有其他内容。如果您在右侧添加操作项,可能需要进行调整。

    @dimen/app_bar_padding_right = 72dp

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:paddingTop="@dimen/app_bar_padding"
                android:paddingBottom="@dimen/app_bar_padding"
                android:paddingLeft="@dimen/app_bar_padding"
                android:paddingRight="@dimen/app_bar_padding_right"
                android:src="@drawable/nav_bar_logo"
                android:contentDescription="@string/app_name" />
    

    【讨论】:

      【解决方案4】:

      将这些属性添加到 ImageView...

              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多