AppCompat v21 Toolbar changing logo size

2019-04-27 11:09发布

问题:

I am migrating to the new Toolbar feature in appcompat v21 from the previous action bar. I still want to keep the logo on the top left part of the actionbar (toolbar). For doing I added in my layout the support toolbar and I created a new thene for it.

    app:theme="@style/NewToolBarStyle"

I am adding the log programmatically as there is some logic in the app for this.

            actionBar.setLogo(R.drawable.myicon);

Referring to my new style (empty for the moment):

<style name="NewToolBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</style>

However the result is showing an image the is too big for what I am looking for, and I am wondering how to reduce the size of the icon.

Is there any way (style, layout or programming) that I can reduce the logo size?

回答1:

There is no logo icon in material design : http://www.google.com/design/spec/layout/structure.html#, so I suppose this is not well tested scenerio - or simply (broken) by design. You can add ImageView as a child widget of your toolbar and use it to show any image. It will show on the right of all the other internal widgets - like spinner - but list navigation mode is also deprecated.

If you insist on having logo then my workaround for this is to make sure toolbar is of fixed height - this takes care of wrong icon height. Even after that you will have to set setAdjustViewBounds to true on toolbars internal logo ImageView - otherwise it will create large left and right padding.

This is how my toolbar looks like (height set to ?attr/actionBarSize):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>

reference it inside your activity layout using:

<include layout="@layout/toolbar_actionbar"/>

dont change layout_height in include.

The second step is to setAdjustViewBounds(true) on logo icon:

    Drawable logo = getDrawable(iconRes);
    toolbar.setLogo(logo);
    for (int i = 0; i < toolbar.getChildCount(); i++) {
      View child = toolbar.getChildAt(i);
      if (child != null)
        if (child.getClass() == ImageView.class) {
          ImageView iv2 = (ImageView) child;
          if ( iv2.getDrawable() == logo ) {
            iv2.setAdjustViewBounds(true);
          }
        }
    }


回答2:

Following the suggestiong give by @brightstar I would like to develop further the answer.

The best way to control the size and position of the Logo in the new Toolbar is by actually not using it. The concept is completely different, what you need to do is create a toolbar from scratch. So you need to make a decision, either you use the layout given by the actionBar or include everything new including the title.

If you stop using the logo but you keep using the title you finally will see that the logo is over the title creating and ood situation.

So, an example of what to do is the following:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/action_bar_background"
        app:theme="@style/NewToolBarStyle"
        android:minHeight="?attr/actionBarSize" />
<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="17dp"
        android:textSize="20sp"
        android:layout_toRightOf="@+id/logo_image"
        android:text="@string/app_name"
        android:textColor="@color/white" />

    <ImageView
        android:id="@+id/logo_image"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:scaleType="centerInside" />

</RelativeLayout>
</FrameLayout>

You create this file as my_toolbar.xml. Notice the following details:

  • I did not include an src of my ImageView because I am changing it dinamically. But works adding it.
  • I used a relative layout for being able to center the icon and the text.
  • I need still to include the Home button.

Later on as described by @brightstar you need to include in the top of your layouts by an include, however.... remember to add an id so that you can refere all your other Views to it.

 <include layout="@layout/toolbar_sharemup"
    android:id="@+id/including" />