Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert xml layer-list to layerdrawable by code
    primarykey
    data
    text
    <p>I am trying to code the following xml by code.</p> <p>----drawable\my_layerdrawable.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item&gt; &lt;bitmap android:src="@drawable/my_image" android:gravity="left"/&gt; &lt;/item&gt; &lt;item android:left="10dp"&gt; &lt;bitmap android:src="@drawable/my_image" android:gravity="left" /&gt; &lt;/item&gt; &lt;item android:top="10dp"&gt; &lt;bitmap android:src="@drawable/my_image" android:gravity="left"/&gt; &lt;/item&gt; &lt;item android:top="10dp" android:left="10dp"&gt; &lt;bitmap android:src="@drawable/my_image" android:gravity="left" /&gt; &lt;/item&gt; &lt;item android:top="20dp"&gt; &lt;bitmap android:src="@drawable/my_image" android:gravity="left"/&gt; &lt;/item&gt; &lt;item android:top="20dp" android:left="10dp"&gt; &lt;bitmap android:src="@drawable/my_image" android:gravity="left" /&gt; &lt;/item&gt; &lt;/layer-list&gt; </code></pre> <p>I coded the following block but it is stretching the images.</p> <pre><code> InsetDrawable[] layers = new InsetDrawable[this.itemCount]; Resources resources = getResources(); ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems); int layerTop = 0; for (int i = 0; i &lt; this.itemCount; i++) { int layerLeft = i % 2 == 1 ? 5 : 0; Drawable dr = resources.getDrawable(R.drawable.my_image); layers[i] = new InsetDrawable(dr, layerLeft, layerTop, -layerLeft, -layerTop); layerTop += i % 2 == 1 ? 10 : 0; } LayerDrawable layerDrawable = new LayerDrawable(layers); imgButton.setImageDrawable(layerDrawable); </code></pre> <p>When I assign the drawable xml to the <code>imgButton</code> It is working correctly no stretch or any change.</p> <pre><code>ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems); imgButton.setImageResource(R.drawable.my_layerdrawable); </code></pre> <p>Do you have any idea to make the layer drawable by code?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload