Resize AppCompat v21 logo

I am moving to a new toolbar in appcompat v21 from the previous action bar. I still want to keep the logo in the upper left of the action bar (toolbar). To do this, I added a support toolbar to my layout, and I created a new one for it.

app:theme="@style/NewToolBarStyle" 

I am adding the log programmatically since there is some logic in the application for this.

  actionBar.setLogo(R.drawable.myicon); 

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

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

However, the result is an image that is too large for what I'm looking for, and I'm wondering how to reduce the size of the icon.

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

+6
source share
2 answers

Following the suggestion g give by @brightstar, I would like to continue with a further answer.

The best way to control the size and location of the logo on the new toolbar is to not actually use it. The concept is completely different, you need to create a toolbar from scratch. Therefore, you need to make a decision, either you use the layout specified by the actionBar, or include everything new, including the title.

If you stop using the logo but continue to use the heading, you will finally see that the logo is above the heading and ozone.

So, an example of what needs to be done 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. Pay attention to the following data:

  • I did not enable src of my ImageView because I am changing it dynamically. But the work is being added.
  • I used relative layout to center the icon and text.
  • I need to enable the home button.

Later, as described by @brightstar, you need to include at the top of your layouts with include, however .... remember to add an identifier so that you can reference all your other Views.

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

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

If you insist on having a logo, then my way to solve this problem is to make sure that the toolbar has a fixed height - this concerns the wrong height of the icon. Even after that, you will need to set setAdjustViewBounds to true on the toolbars of the internal ImageView logo - otherwise it will create large left and right additions.

This is what my toolbar looks like (height is 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> 

link to it in your action layout using:

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

Do not include layout_height in include.

The second step is to setAdjustViewBounds (true) on the 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); } } } 
+7
source

Source: https://habr.com/ru/post/978696/


All Articles