本文介绍了在 Android 4.0+ 上删除操作栏左侧图标周围的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想删除标准 android 4.0+ 操作栏中左侧图标周围的填充.我正在设置图标:

I want to remove the padding around the icon on the left in the standard android 4.0+ action bar. I'm setting the icon with:

getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_action_myapp));

我希望图标垂直填充空间,同时触摸顶部和底部,类似于 soundcloud 应用程序所做的:

And I would like the icon to fill vertically the space, touching both top and bottom, similar to what soundcloud app does:

推荐答案

深入 AOSP 源码,似乎涉及的代码在 com.android.internal.widget.ActionBarView.java.特别是相关部分是内部类ActionBarView$HomeViewonLayout()方法,部分报告如下(第1433-1478行):

Digging into AOSP sources, it seems the code involved is in com.android.internal.widget.ActionBarView.java. In particular the relevant part is the onLayout() method of the inner class ActionBarView$HomeView, partially reported below (lines 1433-1478):

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        ...
        final LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
        final int iconHeight = mIconView.getMeasuredHeight();
        final int iconWidth = mIconView.getMeasuredWidth();
        final int hCenter = (r - l) / 2;
        final int iconTop = Math.max(iconLp.topMargin, vCenter - iconHeight / 2);
        final int iconBottom = iconTop + iconHeight;
        final int iconLeft;
        final int iconRight;
        int marginStart = iconLp.getMarginStart();
        final int delta = Math.max(marginStart, hCenter - iconWidth / 2);
        if (isLayoutRtl) {
            iconRight = width - upOffset - delta;
            iconLeft = iconRight - iconWidth;
        } else {
            iconLeft = upOffset + delta;
            iconRight = iconLeft + iconWidth;
        }
        mIconView.layout(iconLeft, iconTop, iconRight, iconBottom);
    }

相同的小部件使用此布局,定义在 res/layout/action_bar_home.xml:

the same widget use this layout, defined in res/layout/action_bar_home.xml:

<view xmlns:android="http://schemas.android.com/apk/res/android"
  class="com.android.internal.widget.ActionBarView$HomeView"
  android:layout_width="wrap_content"
  android:layout_height="match_parent">
<ImageView android:id="@android:id/up"
           android:src="?android:attr/homeAsUpIndicator"
           android:layout_gravity="center_vertical|start"
           android:visibility="gone"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginEnd="-8dip" />
<ImageView android:id="@android:id/home"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginEnd="8dip"
           android:layout_marginTop="@android:dimen/action_bar_icon_vertical_padding"
           android:layout_marginBottom="@android:dimen/action_bar_icon_vertical_padding"
           android:layout_gravity="center"
           android:adjustViewBounds="true"
           android:scaleType="fitCenter" />
</view>

根据来源,图标显示在 Imageview 中,id=android.R.id.home.上面报告的 onLayout() 方法考虑了布局中定义的 ImageView 边距,不能通过主题/样式覆盖设置,因为它们使用值 @android:dimen/action_bar_icon_vertical_padding.

According to sources the icon is shown in the Imageview with id=android.R.id.home. The onLayout() method reported above takes account of the ImageView margins defined in the layout, which can't be set via theme/style override because they use the value @android:dimen/action_bar_icon_vertical_padding.

你所能做的就是在运行时去掉这些值,并根据你的需要设置它们:只需检索 ImageView 并将其顶部和底部边距设置为 0.像这样:

All you can do is to get rid of those values at runtime, and set them according your needs:simply retrieve the ImageView and set its top and bottom margins to 0. Something like this:

ImageView icon = (ImageView) findViewById(android.R.id.home);
FrameLayout.LayoutParams iconLp = (FrameLayout.LayoutParams) icon.getLayoutParams();
iconLp.topMargin = iconLp.bottomMargin = 0;
icon.setLayoutParams( iconLp );

我刚刚意识到我没有介绍如何摆脱左填充.下面的解决方案.

操作栏上的左填充受向上导航 操作栏图标的行为.当它被禁用时(通过 ActionBar.setDisplayHomeAsUpEnabled(false)),左/上指示器消失了,但也使用了左填充.一个简单的解决方案:

Left padding on actionbar is affected by the Navigating Up behavior of actionbar icon. When that is disabled (via ActionBar.setDisplayHomeAsUpEnabled(false)) the left/up indicator is gone, but a left padding is used as well. A simple solution:

  1. 使用 ActionBar.setDisplayHomeAsUpEnabled(true) 启用操作栏向上导航,以便在布局过程中考虑指示器视图
  2. res/values-v14/styles.xml 中用作向上指示符的 drawable 强制为 null
  1. enable the actionbar up navigation using ActionBar.setDisplayHomeAsUpEnabled(true) in order to consider the indicator view in the layout process
  2. force the drawable used as up indicator in your res/values-v14/styles.xml to null

例如:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
    <item name="android:homeAsUpIndicator">@null</item>
</style>

这篇关于在 Android 4.0+ 上删除操作栏左侧图标周围的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:50