如何在工具栏中居中ImageView?

Spi*_*ail 15 android center toolbar imageview android-actionbar

试图在我的工具栏中居中一个徽标.当我导航到下一个活动时,会出现"up ADVance"图标,它会将我的徽标略微向右推.如何在不删除可用性图标的情况下将我的徽标保留在工具栏的中心?

这是我的工具栏标签

<android.support.v7.widget.Toolbar
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="wrap_content"
android:background="@color/primaryColor"
android:paddingTop="@dimen/app_bar_top_padding"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
app:theme="@style/CustomToolBarTheme">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_android_logo"
    android:layout_gravity="center"
    android:paddingBottom="3dp"/>
 </android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

Ken*_*gić 14

你可以这样做,因为它适合我:

<androidx.appcompat.widget.Toolbar
        android:id="@+id/mainToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="@dimen/abc_action_bar_default_height_material">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/headerLogo"
                android:contentDescription="@string/app_name" />
            </RelativeLayout>

    </androidx.appcompat.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

  • 非常确定如果将菜单项添加到工具栏,这将无效,因为这会缩短RelativeLayout的宽度. (3认同)

小智 9

您需要将 AppBarLayout 小部件与 Toolbar 小部件一起使用,并将 设置contentInsetStart0dp。否则工具栏在开始时会有大约 8dp 的填充(如果需要,插入导航按钮)

这是工作代码:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:fitsSystemWindows="false"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:contentInsetStart="0dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:adjustViewBounds="true"
            android:scaleType="centerInside"
            android:src="@drawable/company_logo" />

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


</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)

另外,请确保您?attr/actionBarSize用作工具栏的高度,因为这是默认的 actionBar 高度,并且会根据所有不同的屏幕尺寸进行调整。


T D*_*yen 5

javadoc中,它说:

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

这意味着除非您创建自定义toolbar或删除自定义,否则无法执行此操作icon.