将图像添加到工具栏的中心

Arc*_*kar 1 android toolbar

我在我的appliaction中使用工具栏而不是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>
Run Code Online (Sandbox Code Playgroud)

Gök*_*ker 5

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

让我们假设你的图像居中是centeree.png,它将被包装在drawable/toolbar_background.xml中

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@android:drawable/centeree"
    android:gravity="center" />
Run Code Online (Sandbox Code Playgroud)

您可以将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">
Run Code Online (Sandbox Code Playgroud)

另外,作为替代方案,您可以使用9个可绘制的贴片,两侧具有相同的可拉伸区域.