除去周围的操作栏填充左图标在Android 4.0+图标、操作、Android

由网友(秋萝茗瑰缘)分享简介:我想删除填充该图标左边的标准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...

我想删除填充该图标左边的标准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源,它似乎参与了code是在com.android.internal.widget.ActionBarView.java.特别是有关的部分是内部类 ActionBarView $ HomeView onLayout()方法,部分报道如下(线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:

<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:扪/ 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(假))的左/上指示走了,但左填充被使用。一个简单的解决办法:

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:

使用使动作条向上导航 ActionBar.setDisplayHomeAsUpEnabled(真),以便考虑在布局过程中,指标视图 在强制用作在你的 RES /值-V14 / styles.xml enable the actionbar up navigation using ActionBar.setDisplayHomeAsUpEnabled(true) in order to consider the indicator view in the layout process 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>
阅读全文

相关推荐

最新文章