android - 如何设置actionBar中logo左右的间距?

标签 android android-actionbar android-toolbar graphical-logo

背景

我想在操作栏(确切地说是工具栏)中 Activity 标题的左侧显示一个位图,所以我使用了这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setLogo(R.mipmap.ic_launcher);
    ...

问题

事实是, Logo 的左侧似乎有太多边距,而标题文本非常靠近 Logo 本身,所以它看起来像这样:

enter image description here

这根本不对称......

如果我有抽屉导航,情况会更糟:

enter image description here

我尝试过的

  1. 我尝试了其他尺寸的图标,另外我尝试使用位图而不是可绘制/mipmap 资源(而且我实际上需要使用位图),如下所示:

     TypedValue tv = new TypedValue();
     if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
         int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
         Bitmap bitmap = Bitmap.createBitmap(actionBarHeight, actionBarHeight, Config.ARGB_8888);
         Canvas canvas = new Canvas(bitmap);
         final Paint paint = new Paint();
         paint.setColor(0xffff0000);
         canvas.drawPaint(paint);
         getSupportActionBar().setLogo(new BitmapDrawable(getResources(),bitmap));
     }
    

但我仍然得到相同的结果:

enter image description here

  • 我尝试使用这个:

     toolbar.setContentInsetsAbsolute(0,0);
    
  • 我尝试在工具栏的 xml 标记中使用它:

     android:contentInsetLeft="0px"
     android:contentInsetStart="0px"
    
  • 我还尝试使用其右侧和末尾的其他值(至少使空格大小相等),但它们似乎根本不影响 Logo 边距。

  • 唯一有帮助的是:

     app:contentInsetStartWithNavigation="0px"
    
  • 但只有当有抽屉导航时它才有帮助:

    enter image description here

    在没有抽屉导航的情况下,我仍然看到额外的空间,这使得 Logo 左侧与右侧的空间看起来不均匀。我尝试将此值设置为负数,但在没有抽屉导航的情况下它不会执行任何操作。

  • 我还尝试使用“布局检查器”工具来调查为什么它有额外的空间,这样我就可以强制它有更少的空间,但我找不到任何填充/边距导致这个:
  • enter image description here

    问题

    如何避免这种行为?如何最小化/设置 Logo 左右两侧的空间?

    为什么会出现这种情况?

    我应该使用自定义 View 吗?

    最佳答案

    How can I minimize/set the space on the left&right of the logo ?

    在工具栏中使用这些属性来处理工具栏 View 的边距。

            android:layout_marginLeft="-16dp"
            app:titleMarginStart="@dimen/your_required_space"
    

    -16dp留下大约 4dp 的空间。

    使用 layout_marginLeft 的缺点是,当 Logo 或自定义位图之前有另一个 View 时,它无法正常工作。例如,抽屉导航中的汉堡图标、启动子 Activity 或打开抽屉导航时的向上插入符号图标(后箭头)。发生这种情况是因为 Logo 被此类 View 替换。

    在这种情况下,您可以使用:

            app:contentInsetStartWithNavigation="0dp"
    

    Why does it occur?

    也许,工具栏类使用已经设置的尺寸,开发人员认为这些尺寸是标准尺寸,并且没有明确的方法来更改它们。

    Should I just use a custom view instead?

    您可以使用自定义 View ,因为您可以完全控制,但使用自定义 View 时,您会遇到自定义问题。

    关于android - 如何设置actionBar中logo左右的间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39941678/

    相关文章:

    android - 如何修复错误 "' WhereIterable<Products >' is not a subtype of type ' List<Products >'"

    android - 避免状态栏在所有屏幕上重叠

    android - CAB "check"按钮

    Android的Toolbar/SupportActionBar Material Design

    android - 如何从左到右工具栏api 16

    Android ViewFlipper ZOrder

    java - Android 包生成器错误 : Unsupported Major Minor version 52. 0

    java - 在 Theme.AppCompat.Light 中添加图标

    android - 如何在布局转换期间为操作栏设置动画?

    java - setFlags 后工具栏与状态栏重叠