android - 在 ActionBar 上正确居中 Logo 的最佳方法是什么?

标签 android menu android-actionbar menuitem

我想要实现的目的是将自定义 Logo 放在 ActionBar 上。我的 ActionBar 有以下自定义布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name"
        android:src="@drawable/logo_header" />
</FrameLayout>

如果右侧没有任何图标/溢出操作按钮,这将给出预期的视觉结果。如果有, Logo 将相对于 ActionBar 上的可用空间居中,这使其看起来不居中,因为 Logo 将显示为左对齐。

高级,我想尝试的是计算屏幕上 ActionBar 的绝对宽度与其可用宽度之间的差异,然后在 Logo 上应用等于该差异值的 layoutMarginLeft 。我想我可以通过获取可见菜单项的数量并将它们乘以它们的宽度来实现这一点(我想我可以找到一个默认尺寸)。

但是,在这样做之前,我想知道是否有一个我忽略的更简单的解决方案?例如,只能通过自定义布局来解决这个问题吗?

谢谢!

最佳答案

我也遇到了同样的问题。我最终使用主题 NoActionabr 主题,然后使用工具栏并将 View 放置在其上,使其居中。

主题

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
    </style>

</resources>

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_journal_main"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/primary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    </android.support.v7.widget.Toolbar>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_marginLeft="72dp"
        android:layout_marginRight="72dp">

        <ImageView
            android:id="@+id/datePrevious"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_centerVertical="true"
            android:background="@drawable/button_flat_selector"
            android:clickable="true"
            android:padding="8dp"
            android:src="@mipmap/ic_chevron_left"/>

        <TextView
            android:id="@+id/tbDate"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Today, April 20"
            android:textColor="@color/white"
            android:textSize="18dp"
            android:textStyle="bold"/>

        <ImageView
            android:id="@+id/dateNext"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@drawable/button_flat_selector"
            android:clickable="true"
            android:padding="8dp"
            android:src="@mipmap/ic_chevron_right"/>
    </RelativeLayout>
</RelativeLayout>

enter image description here

关于android - 在 ActionBar 上正确居中 Logo 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30305160/

相关文章:

c - 选择项目后 Win32 菜单位图透明度

java - 即使创建后表中也没有列

android - 如何通过 FaSTLane 创建 aab (bundle)?

android - 在 Activity Transition 上制作相同的状态栏颜色

android - 将按钮添加到列表 Activity

javascript - React 中带有延迟子菜单 View 的菜单

Qt - 弹出菜单

android - 动态改变操作栏高度

android - invalidateOptionsMenu 在 fragment 中不起作用

android - 在 android 3.2 上的 actionbar 中搜索元素时,findViewById 返回 null