【问题标题】:remove padding around action bar left icon on Android 4.0+在 Android 4.0+ 上删除操作栏左侧图标周围的填充
【发布时间】:2013-04-08 04:57:24
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-layout android-actionbar


    【解决方案1】:

    深入研究 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>
    

    根据消息来源,该图标显示在Imageview 中,id=android.R.id.home。上面报告的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 中用作向上指示器的drawable 强制为null

    例如:

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

    【讨论】:

    • 我还没试过,但它看起来很合理而且写得很好,所以你会得到赏金。我会尽快标记为接受;-)
    • 谢谢@luca ;) 我在发布之前自己测试了代码,所以我相信它也适合你。
    • 我添加了
    • @Hugo 您必须在哪个 API 级别上执行此操作?在 API 级别 >= 14 上将填充设置为 0 后,它对我有用
    • @DanieleRicci android:minSdkVersion="8" android:targetSdkVersion="17"。我使用 ActionbarSherlock,图像视图因版本而异。我这样做: if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GIGERBREAD_MR1) icon = (ImageView) findViewById(android.R.id.home);否则图标 = (ImageView) findViewById(R.id.abs__home);希望它有所帮助:)
    【解决方案2】:

    我发现了另一个改变工具栏样式的解决方案(参考 appcompat-v7 ),代码如下:

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

    【讨论】:

    • 谢谢!!!只有你的解决方案对我有用。接受的答案很好,但在我的情况下,总是显示主页按钮。使用您的解决方案,我得到了清晰的操作栏,我的自定义视图填充了屏幕的宽度。现在我可以用我的宽度和行为向它添加我自己的主页按钮。
    • 请详细说明此解决方案。我的意思是,我需要在哪里添加此代码?我的操作栏有自定义样式。
    • @AndroidGuy 使用上面的主题样式,您想要删除剩余空间的活动,例如:在您的清单活动节点中。使用属性“主题”引用您的自定义主题。然后在您的自定义主题配置项中: @style/Widget.Toolbar 像上面一样。
    • 当您将appcompat支持库更新到v22.1.1时,您必须使用actionBarStyle值来替换toolbarStyle。因为ActionBar的内容插入值没有使用toolbarStyle的属性。
    【解决方案3】:

    为 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>
    

    【讨论】:

    【解决方案4】:

    增强parrzhang回复remove padding around action bar left icon on Android 4.0+

     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);
                }
            }
        }
    

    【讨论】:

    • 这将发出 NullPointerException :(
    【解决方案5】:

    要设置 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 和资源,因此您可以将箭头图标修改为 emty png,以便向上图标移动到最左侧。

    2. 上/后图标来自:

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

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

    不过,这将是一个更大的解决方法。

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

    【讨论】:

    • 感谢您的回答,但它没有回答我的问题 ;-) 你说:> 但这就是重点..怎么样?
    【解决方案6】:

    你可以在 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)查看。

    这样你就可以获取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);
        }
    

    【讨论】: