Reputation: 9020
From this developer guide about Layer-list drawables,
All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate.
In the first sentence, they say that the items are scaled to fit the container view (and Not that the view is scaled according to the size of the items contained in it). Then they say that the size of the container view might increase (which means that the View is being scaled, right?). So doesn't the second sentence contradict the first one? Can somebody explain what is meant there?
android:drawable
Drawable resource. Required. Reference to a drawable resource.
...
To avoid scaling items in the list, use a element inside the element to specify the drawable...
...
For example, the following defines an item that scales to fit its container View:
<item android:drawable="@drawable/image" />
To avoid scaling, the following example uses a element with centered gravity:
<item> <bitmap android:src="@drawable/image" android:gravity="center" /> </item>
Again, they say that android:drawable
is a required attribute, and then they give an example which does not use this attribute. What is correct?
To avoid scaling items in the list, use a
<bitmap>
element inside the<item>
element to specify the drawable and define the gravity to something that does not scale, such as "center"
How is gravity
scalable and how is center
as its value make it unscalable?
Upvotes: 5
Views: 10646
Reputation: 525
In addition to what @Cristopher_Boyd said. The screen density is also important, so different bitmaps should be created for different screen densities. If there is a single image inside the drawable
folder directly, it may not scale properly. Hence, generate different images, or place a single image in drawables-xxhdpi
folder, for example (but not on drawables
). See this answer.
Hope this helps someone,
Xavi
Upvotes: 0
Reputation: 345
It seems that layer-list items are effectively stretched in both X & Y dimensions to fit the container.
You need a drawable for each item. The bitmap child element effectively becomes the drawable for that item.
If you have an empty item (with no drawable resource), it will cause an error when you try to load the layer-list.
Gravity has several options (like FILL
, which is probably the item's default gravity, or FILL_HORIZONTAL
that stretch the item to fill its container).
Additionally, you can set android:gravity="center"
on an item tag itself (without a <bitmap>
child) and it seems to have the same effect (in API 23, at least).
Upvotes: 6