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

标签 android android-actionbar imageview center toolbar

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

这是我的工具栏标签

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

最佳答案

您可以按照对我有用的方式进行操作:

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

关于android - 如何使 ImageView 在工具栏中居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28443786/

相关文章:

java - Android Studio 中的数组 for 循环

java - android-maven-plugin debug 永远等待调试器

android - 沉浸式全屏不隐藏操作栏和状态栏

android - 在设置了最大高度和最大宽度的 ImageView 中定位图像

java - 获取当前日期之前的日期更改 TextView 颜色

android - 如何在 android 中创建自定义操作栏?

android - 更改操作栏微调器文本颜色

java - 当设备从纵向/横向旋转时,如何使 ImageView 旋转图像?

objective-c - UIImageView.image 大于 UIImageView

java - 单例类 : session appears not to be valid sometimes in Android Activity