如何在actionBar中的徽标的左侧和右侧设置空间? [英] How to set space on left&right of the logo in the actionBar ?

查看:121
本文介绍了如何在actionBar中的徽标的左侧和右侧设置空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to show a bitmap on the left side of the title of the activity, in the actionbar (toolbar, to be exact), so I used this:

@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);
    ...

问题

事实是,徽标的左侧似乎有太多的空白,而标题文本距离徽标本身很近,因此看起来像这样:

The problem

Thing is, the logo seems to have too much margin on its left, while the title text is very near the logo itself, so it looks like this:

这根本不是对称的...

This is not symmetrical at all...

如果我有一个导航抽屉,这甚至会更糟:

This is even worse in case I have a nav drawer:

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

  1. I tried other sizes of the icon, plus I tried to use a bitmap instead of a drawable/mipmap resource (and I actually need to use a bitmap anyway), as such:

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

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

But I still got the same result:

我尝试使用此功能:

toolbar.setContentInsetsAbsolute(0,0);

  • 我试图在工具栏的xml标记中使用它:

  • I tried to use this in the xml tag of the toolbar:

    android:contentInsetLeft="0px"
    android:contentInsetStart="0px"
    

    我还尝试在其右边和右边使用其他值(以便至少使空格的大小相等),但是它们似乎根本不影响徽标的边距.

    I also tried to play with the other values to its right&end (in order to at least make the spaces equal in size), but they don't seem to affect the logo margins at all.

    似乎唯一有用的是:

    app:contentInsetStartWithNavigation="0px"
    

    但是它只有在有导航抽屉的情况下才有用:

    But it helps only if there is a navigation-drawer:

    在没有导航抽屉的情况下,我仍然看到额外的空间,这使徽标的左侧与右侧相比显得不均匀.我试图将此值设置为负值,但是在没有导航抽屉的情况下它什么也没做.

    In the case of no navigation drawer, I still see extra space, which makes it look un-even spaces on the left of the logo compared to on its right. I tried to set this value to be negative, but it doesn't do anything in the case of no nav-drawer.

    我还尝试使用布局检查器"工具调查为什么它有额外的空间,以便我可以强迫它具有更少的空间,但找不到任何填充/会导致此的边距:

    I've also tried to investigate why it has the extra space, using the "layout inspector" tool, so that I might be able to force it to have less space, but I couldn't find any padding/margins that will cause this:

    问题

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

    The question

    How do I avoid this behavior? How can I minimize/set the space on the left&right of the logo ?

    为什么会发生?

    我应该改用自定义视图吗?

    Should I just use a custom view instead?

    推荐答案

    如何最小化/设置徽标左右两侧的空间?

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

    在工具栏中使用这些属性来处理工具栏视图的边距.

    Use these attributes in your toolbar to handle margins of the toolbar view.

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

    -16dp保留大约4dp的空间.

    -16dpleaves about 4dp space.

    使用layout_marginLeft的缺点是,当徽标或您的自定义位图之前有另一个视图时,它不能很好地工作.例如,对于导航抽屉,为汉堡图标,在启动子活动或打开导航抽屉时为向上箭头图标(后箭头).发生这种情况是因为徽标已被此类视图取代.

    The disadvantage of using layout_marginLeft is that it does not work well when there is another view before the logo or your custom bitmap. For example, hamburger icon in case of Navigation Drawer, up-caret icon (back-arrow) when child activity is launched or navigation drawer is opened. This happens because the logo gets replaced by such views.

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

    In such case, you can use :

            app:contentInsetStartWithNavigation="0dp"
    

    为什么会发生?

    Why does it occur?

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

    Maybe, toolbar class uses already set dimensions which the developers think are the standard dimensions and there is no explicit way to alter them.

    我应该改用自定义视图吗?

    Should I just use a custom view instead?

    您可以在完全控制的情况下使用自定义视图,但是有了自定义视图,您会遇到自定义难题.

    You can use custom views as you've complete control but with custom views, you get custom headaches.

    这篇关于如何在actionBar中的徽标的左侧和右侧设置空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆