Using the layer-list below, my scale drawable is never shown. Why is that?
menu_dialog_header.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="@color/black"></solid>
</shape>
</item>
<item>
<scale
android:scaleHeight="50%"
android:scaleWidth="100%"
android:scaleGravity="top"
android:drawable="@drawable/shine" />
</item>
</layer-list>
shine.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/shine" />
</shape>
menu_dialog.xml
<!-- header -->
<LinearLayout
android:background="@drawable/menu_dialog_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
android:padding="8dp"
android:orientation="horizontal" >
...
I have a workaround that may or may not work for you: instead of using <scale>
, try <inset>
. You can use a combination of the <item>
's android:top
/ android:bottom
/ android:left
/ android:right
and the <inset>
's android:insetXXX to size the image
You can do things like set the sides of each inset value relative to your source image's dimensions. For example, if you have a 64 pixel-wide image and want to cut it down by 50%, you can do (64 / 4 = 16) for each side
It's not exactly the LayerLists's fault. ScaleDrawable additionally depends on the drawable's level, as (briefly) mentioned both in the Drawable Resources and the ScaleDrawable reference.
It seems there is no documentation of how ScaleDrawable will interpret the level.
There seems to be no way to set a drawable's level in XML, which reflects the fact that the primary use case for ScaleDrawable is to animate a drawable's size. Hence, you'll have to set the drawable's level in your code.
I strongly sympathize with your usage of ScaleDrawable here, because it allows for a more powerful way of stacking drawables by being able to position them and define their size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With