android how to change <item> width and height
I hope this put you on 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>
doesn't have layout_width
/layout_height
attributes.
TL;DR: Refer to my newer, more concise answer.
Actually, unlike it's been said, there are width and height attributes for <item>
as well. Thanks to @Entreco for posting a demonstration of that.android:width
and android:height
for Drawable
s are similar to android:layout_width
and android:layout_height
for View
s, unlike they can be set to neither match_parent
nor wrap_content
, but you can achieve the same behavior of match_parent
by setting the attribute android:gravity
to either left|right
or start|end
(to match the parent's width), or top|bottom
(to match the parent's height).
Unfortunately, those attributes are only available starting from API level 23.
However, considering the above method I suggested in place of match_parent
and that android:width
and android:height
attributes are available for <size>
element (which has to be put inside a <shape>
) without the need of a newer API level, you could 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 having a sized <item>
(with a transparent <shape>
) and an unsized one (with a <bitmap>
) that has android:gravity
set to left|top|right|bottom
for matching both parent's dimensions. So, the real size of the <bitmap>
will be determined by that of the unique sized <item>
in the same parent.
EDIT:
Thanks to @Emil S for noticing that the above solution won't work when used as a window background. I can guess that at the time when the system creates a window with the background specified by the android:windowBackground
attribute, no layout is performed as no process is started yet. So, Android will end up rasterizing the drawable at the screen size and stretch it to fill the whole window, I think. This would explain how that could happen. Indeed, a possible solution that I thought of, but I haven't tested yet, which unfortunately would be supported only starting from API level 24, is to use a custom drawable as a window background. It is possible by referencing an XML resource that uses a custom drawable, like this. This way, the issue should be solved since Android is forced to start the app process before showing the window, and so the drawable should be correctly laid out since it's aware of the window dimensions. This would lead, however, to an almost unnoticeable yet existing delay at the startup.
EDIT:
@João Carlos pointed out that my solution won't work (as it would cause a cyclic inheritance) when using an adaptive icon (those icons made by a background and a foreground, which support vector drawables). However, his point makes no sense, because adaptive icons require API 26: android:width
and android:height
attributes may be used on the splash screen or something, as they require API 23.
So, in order to get anything to work in any version of Android, you'll need to differentiate two drawables, the former for API < 23, using the solution I posted, and the latter for API >= 23, using the two attributes as I said at the beginning of this post.
This is kind of a workaround, but it worked for me.
You can use padding in order to make the second drawable smaller.
<item android:drawable="@drawable/circyle" />
<item
android:drawable="@drawable/plus"
android:top="10dp"
android:bottom="10dp"
android:right="10dp"
android:left="10dp" />