Search code examples
androidandroid-viewandroid-drawablelayer-list

Drawable XML height not preserved by View?


I have a drawable xml mybackground.xml as below with a height of 50dp

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:endColor="#00FF00"
                android:startColor="#00FFFF" />
            <size android:height="50dp" />
        </shape>
    </item>
</layer-list>

If I use it as below in ImageView

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/mybackground"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

or in FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/mybackground"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

It is shown as

enter image description here

However if I use View instead,

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/mybackground"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

it is shown as

enter image description here

The View is not preserving the height defined on the mybackground.xml. Did I miss anything?


Solution

  • I looked at your question and was curious to compare the View class with the other classes. As a result, there was no part to calculate the size of the background in the view class measure (), and in the measure () of the FrameLayout, ImageView, etc., the dimension was determined considering the size of the background. This seems to be a simple conceptual issue.