在Android 4.0及以上版本中,去除操作栏左侧图标周围的填充。

26

我想要移除标准 Android 4.0+ 操作栏左侧图标周围的填充。我正在使用以下方法设置图标:

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

我希望图标垂直填充空间,同时触及顶部和底部,类似于SoundCloud应用程序的效果:

6个回答

29
深入研究AOSP源代码,似乎涉及的代码位于com.android.internal.widget.ActionBarView.java中。特别是内部类ActionBarView$HomeViewonLayout()方法,部分报告如下(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>

根据消息来源,图标显示在具有id=android.R.id.homeImageview中。上述提到的onLayout()方法考虑了在布局中定义的ImageView边距,这些边距不能通过主题/样式覆盖设置,因为它们使用值@android:dimen/action_bar_icon_vertical_padding
你所能做的就是在运行时摆脱这些值,并根据需要进行设置:只需检索ImageView并将其顶部和底部边距设置为0。就像这样:
ImageView icon = (ImageView) findViewById(android.R.id.home);
FrameLayout.LayoutParams iconLp = (FrameLayout.LayoutParams) icon.getLayoutParams();
iconLp.topMargin = iconLp.bottomMargin = 0;
icon.setLayoutParams( iconLp );

编辑:我刚意识到我没有讲述如何去除左边填充。下面是解决方案。

操作栏的左填充受到操作栏图标的Navigating Up行为的影响。当禁用它(通过ActionBar.setDisplayHomeAsUpEnabled(false))时,左/上指示器消失了,但也使用了左填充。一个简单的解决方法:

  1. 使用ActionBar.setDisplayHomeAsUpEnabled(true)启用操作栏向上导航以考虑指示器视图在布局过程中
  2. 在您的res/values-v14/styles.xml中强制使用作为向上指示器的可绘制对象为null

例如:

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

1
我还没有尝试过,但它看起来合理且写得很好,所以你会得到奖励。我会在有时间尝试后标记为已接受;-) - luca
感谢 @luca ;) 在发布之前,我已经亲自测试了这段代码,所以我有信心它也会对你起作用。 - a.bertucci
在我添加了<iconLp.height=iconLp.FILL_PARENT; iconLp.width=iconLp.FILL_PARENT;>之后,它对我起作用了。 - Hugo Matilla
@Hugo,你是在哪个API级别上做的?在API级别>=14上将padding设置为0后,它对我有效。 - Daniele Ricci
1
@DanieleRicci android:minSdkVersion="8" android:targetSdkVersion="17"。我使用ActionbarSherlock,ImageView根据版本不同而不同。我这样做:if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) icon = (ImageView) findViewById(android.R.id.home); else icon = (ImageView) findViewById(R.id.abs__home); 希望能帮到你 :) - Hugo Matilla
@Hugo 谢谢。我最终使用了 AppCompat ActionBar 做了同样的事情。很遗憾 Google 没有提供任何“标准”方法来实现这一点。 - Daniele Ricci

20

我发现另一个解决方案(参考 appcompat-v7 库)可以改变 toolbarStyle,代码如下:

<item name="toolbarStyle">@style/Widget.Toolbar</item>


<style name="Widget.Toolbar" parent="@style/Widget.AppCompat.Toolbar">
    <item name="contentInsetStart">0dp</item>
</style>

谢谢!!!只有你的解决方案对我起作用。被接受的答案很好,但在我的情况下,主页按钮总是显示。通过你的解决方案,我得到了清晰的操作栏,并填充了屏幕宽度的自定义视图。现在我可以添加自己的主页按钮,并设置它的宽度和行为。 - lobzik
1
请更详细地解释一下这个解决方案。我的意思是,我需要在哪里添加这段代码?我对我的操作栏有自定义样式。 - AndroidGuy
@AndroidGuy 使用上述主题样式从您想要删除左侧空间的活动开始,例如: 在您的清单活动节点中。使用属性“theme”引用您的自定义主题。 然后,在您的自定义主题配置项中: <item name="toolbarStyle">@style/Widget.Toolbar</item>就像上面一样。 - jhondge
当您将appcompat支持库更新到v22.1.1时,您必须使用actionBarStyle值来替换toolbarStyle。因为ActionBar内容插入值不使用toolbarStyle的属性。 - jhondge

11

使用自定义布局来定制 ActionBar

在此输入图片描述

public class TestActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ActionBar actionBar = getActionBar();
        actionBar.setCustomView(R.layout.actionbar_custom_view_home);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);

        setContentView(R.layout.main);

    }

    public void Click(View v) {
        if (v.getId() == R.id.imageIcon) {
            Log.e("click on-->  ", "Action icon");
        }
    }
}

actionbar_custom_view_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <ImageView
        android:id="@+id/imageIcon"
        android:onClick="Click"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Icon With Title"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

这是自定义操作栏视图的样板代码,但它没有移除左侧的填充/边距。请参见https://dev59.com/rV8d5IYBdhLWcg3wzEv4?lq=1 - s-hunter

2

Android 4.0及以上版本中去除操作栏左侧图标周围的填充中,增强了parrzhang的回答。

 private void adjustHomeButtonLayout(){
        ImageView view = (ImageView)findViewById(android.R.id.home);
        if(view.getParent() instanceof ViewGroup){
            ViewGroup viewGroup = (ViewGroup)view.getParent();
            View upView = viewGroup.getChildAt(0);
            if(upView != null && upView.getLayoutParams() instanceof FrameLayout.LayoutParams){
                FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) upView.getLayoutParams();
                layoutParams.width = 20;// **can give your own width**
                upView.setLayoutParams(layoutParams);
            }
        }
    }

这将会引发一个空指针异常 :( - Hamzeh Soboh

0

要设置ActionBar的高度,您可以创建一个新的主题,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>   
    <style name="Theme.BarSize" parent="Theme.Sherlock.Light.DarkActionBar">
        <item name="actionBarSize">48dip</item>
        <item name="android:actionBarSize">48dip</item> 
    </style> 
 </resources>

并将此主题设置为您的活动:

android:theme="@style/Theme.BarSize"

现在,将图标的高度设置为"match_parent"

这将删除顶部和底部填充。

现在,左侧的箭头是内置到框架中的,因此您有两个解决方法:

  1. 使用ActionBarSherlock。它使用自己的drwables和资源,因此您可以将箭头图标修改为一个空的png,这样您的向上图标就会移动到极左边。

  2. 向上/返回图标来自于:

public boolean onOptionsItemSelected(MenuItem item) 
               {
          switch (item.getItemId()) 
                {
                case android.R.id.home:
                            NavUtils.navigateUpFromSameTask(this);
              return true;
                }      
               }

因此,您可以创建另一个操作栏选项,它具有用于返回上一个活动的意图,然后将该图标放置在操作栏中,而不是使用此选项作为向上按钮。

虽然这样做需要更多的工作,但这可以解决问题。

希望能对您有所帮助.. :)


谢谢您的回答,但它并没有回答我的问题;-) 您说:<<现在,将图标的高度设置为“match_parent”>> 但这就是问题所在..如何实现? - luca

-1

你可以在actionbarview中定义homeview,像这样:

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

但是你不能通过findViewById(android.R.id.up)获取upView。

所以你可以获取homeView并获取其父视图,将upView的宽度设置为0。

    ImageView view = (ImageView)findViewById(android.R.id.home);
    if(view.getParent() instanceof ViewGroup){
        ViewGroup viewGroup = (ViewGroup)view.getParent();
        View upView = viewGroup.getChildAt(0);
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) upView.getLayoutParams();
        layoutParams.width = 0;
        upView.setLayoutParams(layoutParams);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接