android - 带有 customView 的 Actionbar 有额外的填充,无法删除

标签 android

无法让 ActionBar 以正确的样式显示图标。

我的客户想要一个具有不同透明度的特定图标的 ActionBar。如果我只是将图标放入 res/menu 中的 XML 文件中,那么我无法让图标彼此相邻,并且每个图标的背景会导致垂直间隙,如下所示:

enter image description here

res/menu/global_nav.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/menu"
    android:icon="@drawable/ic_main_menu"
    android:showAsAction="always"
    android:title="@string/nav_menu_main"/>  

<item
    android:id="@+id/login"
    android:icon="@drawable/ic_people"
    android:showAsAction="always"
    android:title="@string/nav_menu_login"/>
<item
    android:id="@+id/location"
    android:icon="@drawable/ic_location"
    android:showAsAction="always"
    android:title="@string/nav_menu_location"/>

等等等等

因此,我创建了一个 customView 并将相同的图标简单地放置在 RelativeLayout 中。这使得图标排列得更好并且不会在图标之间留下间隙。我可以更轻松地设置样式,即使我无法像上面那样使用菜单。但是,它在 PagerTitleStrip 上方的图标行底部留下了一个空隙。 UX 图像人们在图标周围留下了完全透明的搜索图标,但所有其他图标都有一些浅色,这使得图标下方的额外填充非常明显。如果周围没有轻微的透明度,人们可能永远不会注意到,但我的客户希望这种填充消失。

enter image description here

res/layout/global_nav_custom.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/att_blue"
android:paddingBottom="-54dp"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:src="@drawable/ic_main_menu" />

<ImageView
    android:id="@+id/search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="false"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_search" />

<ImageView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/search"
    android:src="@drawable/ic_message" />

<ImageView
    android:id="@+id/cart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/message"
    android:src="@drawable/ic_cart" />

<ImageView
    android:id="@+id/location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/cart"
    android:src="@drawable/ic_location" />

<ImageView
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/location"
    android:src="@drawable/ic_people" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/menu"
    android:layout_marginLeft="364dp"
    android:layout_toRightOf="@+id/menu"
    android:text="@string/app_name"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/white" />

</RelativeLayout>

Java代码

    ActionBar bar = getActionBar();
    bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    LayoutInflater li = LayoutInflater.from(this);
    View customView = li.inflate(R.layout.global_nav_custom, null);


    bar.setCustomView(customView);

因此,我的问题是 A) 如何设置 ActionBar 的样式以使用标准样式 XML 作为菜单但删除垂直间隙。或者 B) 使用 customView 解决方案,底部出现 4+ 像素间隙。

顺便说一句,ActionBarSherlock 不是一个选项,因为公司不需要第 3 方库

最佳答案

使用标准操作按钮

您可以通过为您的应用使用自定义主题,使用标准操作按钮实现此目的。
注意:要使其正常工作,您的图标必须小于等于 32dp

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="android:Theme.Holo.Light">
        <item name="android:actionButtonStyle">@style/ActionButton</item>
        <item name="android:actionBarSize">32dp</item>
    </style>

    <style name="ActionButton" parent="android:Widget.ActionButton">
        <item name="android:paddingStart">0dp</item>
        <item name="android:paddingEnd">0dp</item>
        <item name="android:paddingLeft">0dp</item>
        <item name="android:paddingRight">0dp</item>
        <item name="android:minWidth">0dp</item>
    </style>
</resources>

enter image description here

不要忘记编辑您的 AndroidManifest 以设置您的 Activity/应用程序的主题 ;)


使用 actionLayout

另一种方法是为每个操作项指定一个actionLayout。这样您就可以随心所欲地保留图标,并将操作栏大小保持在 48dp

缺点是您必须通过使用 OnClickListener 或使用 onClick View 的属性。 MenuItemonClick 属性将不起作用。

您可能还应该在用户长按项目时显示菜单标题,就像标准操作按钮一样。

菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/menu"
        android:actionLayout="@layout/widget_main_button"
        android:showAsAction="always"
        android:title="main"/>
    <item
        android:id="@+id/login"
        android:actionLayout="@layout/widget_login_button"
        android:showAsAction="always"
        android:title="login"/>
    <item
        android:id="@+id/location"
        android:actionLayout="@layout/widget_location_button"
        android:showAsAction="always"
        android:title="location"/>
</menu>

widget_main_button.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_main_button"
    android:layout_width="@dimen/action_button_width"
    android:layout_height="?android:actionBarSize"
    android:background="@drawable/ic_main_menu"/>

enter image description here


设置 android 应用程序样式时的一个有用工具是 Android Resource Navigator杰夫吉尔费尔特。这是一个 Chrome 扩展程序,可让您快速浏览默认的 Android 主题和样式以找到您需要更改的内容。

关于android - 带有 customView 的 Actionbar 有额外的填充,无法删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16626017/

相关文章:

java - Firestore 无法将 java.lang.String 类型的值转换为日期

android - 修复了可见的标签/点 TabLayout android

android - Recyclerview 不显示警报对话框中的数据列表

android - 在android源代码中包含一个包

android - 在 Lollipop 崩溃前使用 android vector Drawables

android - Android边距开始/结束和右/左有什么区别?

android - 如何从 ListView 获取单选按钮位置在android中单击按钮

java - 在 Azure AD B2C 注册页面上添加自定义电话号码字段

java - Android在Java中超时重复任务

android - 位置跟踪 - 带计步器和 WIFI 的西格玛点卡尔曼滤波器