Android how to change element width and height

I am making a <layer-list> for drawing.

I have a background image and I want the second layer to be smaller, but it doesn't seem to matter what I write inside my android:layer_width and android:layer_height .

The size of the second layer remains the same.

Here is my XML:

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <item android:drawable="@drawable/picuser" android:layout_width="50dp" android:layout_height="50dp" /> <item android:drawable="@drawable/ic_launcher" android:layout_width="10dp" android:layout_height="10dp" /> </layer-list> 
+32
android xml
May 22 '12 at 17:16
source share
6 answers

I hope this guides you in the right direction:

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/picuser"/> <item> <bitmap android:src="@drawable/ic_launcher" android:gravity="center" /> </item> </layer-list> 

As you can see here , <item> does not have layout_width / layout_height .

+18
May 22 '12 at 17:58
source share

This is kind of a workaround, but it worked for me.
You can use the indentation to make the second drawing smaller.

 <item android:drawable="@drawable/circyle" /> <item android:drawable="@drawable/plus" android:top="10dp" android:bottom="10dp" android:right="10dp" android:left="10dp" /> 

+30
Dec 19 '13 at 13:00
source share

Unlike the one who said, there are also width and height attributes for <item> . Thanks to @Entreco for posting a demonstration of this.
android:width and android:height for Drawable similar to android:layout_width and android:layout_height for View c, unlike they cannot be set to either match_parent or wrap_content , but you can achieve the same match_parent behavior by setting attribute android:gravity value left|right or start|end (to match the parent width) or top|bottom (to match the parent height).

Unfortunately, these attributes are only available after API 23.

However, given the above method, which I suggested instead of match_parent and that the android:width and android:height attributes are available for the <size> element (which should be placed inside the <shape> ) without any API restrictions, you can use a simple workaround :

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <size android:width="152dp" android:height="152dp"/> <solid android:color="#00FFFFFF"/> </shape> </item> <item> <bitmap android:gravity="left|right|top|bottom" android:src="@mipmap/ic_launcher"/> </item> </layer-list> 

The above solution takes advantage of the size of <item> (with transparent <shape> ) and non-standard (with <bitmap> ), for which android:gravity set to left|top|right|bottom to match both parent dimensions. Thus, the actual size of the <bitmap> will be determined by the size of the unique size of the <item> in the same parent element.

EDIT:

Thanks to @Emil S, who noticed that the above solution will not work as the background of the window. It only works as a background or foreground of any kind. I don’t know the specific reason for this strange behavior, but I can assume that while the system creates a window with the background specified in the android:windowBackground , the layout does not work, since the process has not been started yet. So Android will eventually rasterize the stretchable image to the screen size and stretch it to fill the whole window, I think. This explains how this could happen.

EDIT:

@ Joao Carlos pointed out that my solution will not work (because it will cause circular inheritance) when using the adaptive icon (icons made by background and foreground that support vector graphics). However, his point of view is pointless, since adaptive icons require API 26: you can use android:width and android:height directly on the splash screen or something like that (they just need API 23).
Therefore, in order for something to work in any version of Android, you need to distinguish between two drawn elements, the first for the API & lt; 23, using the solution I posted, and the last for the API> = 23, using two attributes, as I said at the beginning of my post.

+13
Sep 08 '17 at 11:41 on
source share

Starting from API level 23 and above, you can set the width and height of an element

 <item android:drawable="@drawable/splash_logo" android:height="100dp" android:width="100dp"/> 

NOTE. Use android:width and android:height instead of android:layout_width and android:layout_height

+10
Jan 05 '17 at 8:15
source share

I tried a lot, but could not find a reliable way to center the logo in the XML layout. Finally, I found the following workaround:

 mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { ImageView imageView = (ImageView)mToolbar.findViewById(R.id.logo); if (mToolbar == null) return; int toolbarWidth = mToolbar.getWidth(); int imageWidth = imageView.getWidth(); imageView.setX((toolbarWidth - imageWidth) / 2); } }); 

layout_toolbar.xml:

 <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" style="@style/Toolbar" android:layout_width="match_parent" android:layout_height="@dimen/toolbar_height" android:background="@drawable/toolbar_background" android:focusable="false" app:titleTextAppearance="@style/AppTheme.Toolbar.Title"> <ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/toolbar_logo" /> </android.support.v7.widget.Toolbar> 
+2
Jun 16 '16 at 6:55
source share

Try this:

 <?xml version="1.0" encoding="utf-8"?> <!-- The android:opacity="opaque" line is critical in preventing a flash of black as your theme transitions. --> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"> <!-- The background color, preferably the same as your normal theme --> <item android:drawable="@android:color/white" /> <item android:height="100dp" android:width="100dp" android:gravity="center"> <bitmap android:src="@drawable/ic_launcher_bc_512"/> </item> </layer-list> 
+2
Sep 10 '18 at 15:57
source share



All Articles