How to set space on left&right of the logo in the actionBar ?

丶灬走出姿态 提交于 2019-12-12 09:14:20

问题


Background

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:

What I've tried

  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:

  2. I tried to use this:

    toolbar.setContentInsetsAbsolute(0,0);
    
  3. 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.

  4. Only thing that seem to help is this:

    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.

  5. 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 ?

Why does it occur?

Should I just use a custom view instead?


回答1:


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"

-16dpleaves about 4dp space.

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.



来源:https://stackoverflow.com/questions/39941678/how-to-set-space-on-leftright-of-the-logo-in-the-actionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!