Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can create a resource (layer drawable xml) for your <code>ImageView</code>'s "border" (actually background), and declare in your <code>theme</code> that the <code>ImageView</code>'s background resource is the drawable xml.</p> <p>If you need this "border" to be changed based on the <code>ImageView</code>'s state (<code>focused</code>, <code>selected</code>, etc.), then you should create more layer drawables, and put them together into a selector xml (state drawable). <br /> Then in your theme you should set the <code>ImageView</code>'s background to be this <code>selector.xml</code>.</p> <p><strong>Update</strong><br /> Below is a sample of how to specify a simple border to your images, that will result in</p> <p><img src="https://i.stack.imgur.com/tz0h0.jpg" alt="efteling with border"></p> <p>You have to </p> <ol> <li>create a new layer drawable file (<code>image_border.xml</code>),</li> <li>modify/create your <code>styles.xml</code> file</li> <li>modify/create your <code>colors.xml</code> file</li> <li>modify your layout xml file (or your code) to apply the style to the <code>ImageView</code>.</li> </ol> <p><strong>res/drawable/image_border.xml</strong></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;shape android:shape="rectangle"&gt; &lt;gradient android:angle="90" android:startColor="@color/image_border_start" android:centerColor="@color/image_border_center" android:endColor="@color/image_border_end" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item android:top="2dp" android:left="2dp" android:right="2dp" android:bottom="2dp"&gt; &lt;shape android:shape="rectangle"&gt; &lt;solid android:color="@color/default_back_color" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;/layer-list&gt; </code></pre> <p><strong>res/values/styles.xml</strong><br /> Add the following lines:</p> <pre><code>&lt;style name="myImageView"&gt; &lt;!-- 3dp so the background border to be visible --&gt; &lt;item name="android:padding"&gt;3dp&lt;/item&gt; &lt;item name="android:background"&gt;@drawable/image_border&lt;/item&gt; &lt;item name="android:scaleType"&gt;fitCenter&lt;/item&gt; &lt;/style&gt; </code></pre> <p><strong>res/values/colors.xml</strong><br /> Add the following lines:</p> <pre><code>&lt;color name="image_border_start"&gt;#40990000&lt;/color&gt; &lt;color name="image_border_end"&gt;#FF660000&lt;/color&gt; &lt;color name="image_border_center"&gt;#FFFF3333&lt;/color&gt; </code></pre> <p>And finally specify the style of your <code>ImageView</code> in your <strong>layout xml</strong>:</p> <pre><code>&lt;ImageView android:id="@+id/my_image" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/efteling" style="@style/myImageView" /&gt; </code></pre>
 

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