如何使 ImageView 在工具栏中居中?

How to center ImageView in a toolbar?

一直在尝试将徽标置于工具栏的中央。当我导航到下一个 activity 时,会出现 "up affordance" 图标,它会将我的徽标稍微向右推。我怎样才能让我的标志保持在工具栏的中央,而不删除向上的启示图标?

这是我的工具栏标签

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

javadoc中说:

One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.

这意味着除非您创建自定义 toolbar 或删除 icon.

,否则您无法执行此操作

将图像设置为工具栏的背景,而不是子视图。

<android.support.v7.widget.Toolbar
    android:id="@+id/detail_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:background="@drawable/my_image"
    app:theme="@style/CustomToolBarTheme">

你可以按照下面的方法来做它对我有用:

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

您可以通过编程方式完成,这是我设法使徽标在工具栏中完全居中的唯一方法。

加你activity:

@Override
public void onWindowFocusChanged(boolean hasFocus){

    // set toolbar logo to center programmatically
    Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
    ImageView logo = (ImageView) findViewById(R.id.logo);
    int offset = (toolbar.getWidth() / 2) - (logo.getWidth() / 2);
    // set
    logo.setX(offset);

}

和你的toolbar_main.xml:

<?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/main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/bg_yellow"
    android:elevation="4dp"
    android:fontFamily="@string/font_family_thin"
    app:contentInsetStartWithNavigation="0dp"
    android:layout_gravity="center_horizontal">

    <TextView
        android:id="@+id/title_toolbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@string/font_family_light"
        android:textColor="@color/text_dark_xxx"
        android:textSize="@dimen/text_default"
        android:layout_centerVertical="true"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/logo"
        android:id="@+id/logo"
        android:paddingTop="5dp" />



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

我来晚了回答这个问题,但有很好的解决方案

 <android.support.design.widget.AppBarLayout 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:theme="@style/AppTheme.AppBarOverlay">
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:contentInsetStart="0dp"
        app:contentInsetEnd="0dp"
        app:popupTheme="@style/AppTheme.PopupOverlay">



    </android.support.v7.widget.Toolbar>
    <RelativeLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content">


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="?attr/actionBarSize"
            android:contentDescription="@string/app_name"
            android:id="@+id/logoImageView"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:src="@drawable/toolbar_background" />
    </RelativeLayout>
</FrameLayout>


</android.support.design.widget.AppBarLayout>

这是我的 toolbar_background.xml 可绘制对象

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/colorPrimary" />
    </shape>
</item>
<item>
    <bitmap
        android:src="@drawable/splash" android:gravity="center"
        />
</item>

对于使用矢量绘图SVG 图像 的开发者。
这可以作为工具栏的背景,drawable_toolbar_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/primary"/>
        </shape>
    </item>
    <item
        android:width="@dimen/toolbar_app_icon_width"
        android:height="@dimen/toolbar_app_icon_height"
        android:drawable="@drawable/ic_app_logo"
        android:gravity="center"/>
</layer-list>

是的,你必须固定drawable中item的宽度和高度,尽管vector drawable已经有固定的尺寸。
这是您的工具栏视图,

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/drawable_toolbar_background"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"/>

您需要将 AppBarLayout 小部件与工具栏小部件一起使用,并将 contentInsetStart 设置为 0dp。否则工具栏将在开始处填充大约 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>

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

即使存在菜单操作也能工作的最简单解决方案是:

 <android.support.design.widget.AppBarLayout
    android:id="@+id/apbMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tlbMain"
        android:layout_width="match_parent"
        android:layout_height="?android:actionBarSize"
        app:navigationContentDescription="@null"
        app:title="@null"
        app:subtitle="@null"
        app:popupTheme="@style/AppTheme.PopupOverlay">

            <ImageButton
                android:id="@+id/btnBack"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="12dp"
                android:paddingBottom="12dp"
                android:scaleType="centerInside"
                android:background="@null"
                android:onClick="onBackClickListener"
                android:src="@drawable/ic_navigation_back"
                android:visibility="@{showBackBtn ? View.VISIBLE : View.INVISIBLE}" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_gravity="center"
                android:src="@drawable/toolbar_icon" />

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

</android.support.design.widget.AppBarLayout>

将属性 android:contentInsetStart0dp 放在工具栏中。

<android.support.v7.widget.Toolbar
    ...
    ...
    android:contentInsetStart="0dp">