如何为ImageView设置边框并在Android中更改其颜色?
#1楼
这是我认识的一篇旧帖子,但我认为这可能会帮助那些人。
如果要模拟不与形状的“实心”颜色重叠的半透明边框,请在xml中使用此边框。 请注意,我根本不使用“stroke”标签,因为它似乎总是与实际绘制的形状重叠。
1 <?xml version="1.0" encoding="utf-8"?> 2 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 3 4 <item> 5 <shape android:shape="rectangle" > 6 <solid android:color="#55111111" /> 7 8 <padding 9 android:bottom="10dp" 10 android:left="10dp" 11 android:right="10dp" 12 android:top="10dp" /> 13 14 <corners android:radius="5dp" /> 15 </shape> 16 </item> 17 <item> 18 <shape android:shape="rectangle" > 19 <padding 20 android:bottom="5dp" 21 android:left="5dp" 22 android:right="5dp" 23 android:top="5dp" /> 24 25 <solid android:color="#ff252525" /> 26 </shape> 27 </item> 28</layer-list>
#2楼
我发现这样做容易得多:
1)编辑框架以使内容具有内容(使用9patch工具)。
2)将ImageView放置在Linearlayout ,并将所需的帧背景或颜色设置为Linearlayout的背景。 当您将框架设置为内部内容时,您的ImageView将位于框架内(使用9patch工具设置内容的位置)。
#3楼
xml文件中的ImageView
1<ImageView 2 android:id="@+id/myImage" 3 android:layout_width="100dp" 4 android:layout_height="100dp" 5 6 android:padding="1dp" 7 android:scaleType="centerCrop" 8 android:cropToPadding="true" 9 android:background="@drawable/border_image" 10 11 android:src="@drawable/ic_launcher" />
保存下面的代码名为border_image.xml,它应该在drawable文件夹中
1<?xml version="1.0" encoding="utf-8"?> 2<shape xmlns:android="http://schemas.android.com/apk/res/android" 3 android:shape="rectangle"> 4<gradient android:startColor="#ffffff" 5android:endColor="#ffffff" 6android:angle="270" /> 7<corners android:radius="0dp" /> 8<stroke android:width="0.7dp" android:color="#b4b4b4" /> 9</shape>
如果要将圆角设置为图像边框,则可以更改border.xml文件中的一行
<corners android:radius="4dp" />
#4楼
你必须在res / drawable这段代码中创建一个background.xml
1<?xml version="1.0" encoding="UTF-8"?> 2<shape xmlns:android="http://schemas.android.com/apk/res/android"> 3<solid android:color="#FFFFFF" /> 4<corners android:radius="6dp" /> 5<stroke 6 android:width="6dp" 7 android:color="@android:color/white" /> 8<padding 9 android:bottom="6dp" 10 android:left="6dp" 11 android:right="6dp" 12 android:top="6dp" /> 13</shape>
#5楼
以下是解决这个漫长问题的最简单方法。
1<FrameLayout 2 android:layout_width="112dp" 3 android:layout_height="112dp" 4 android:layout_marginLeft="16dp" <!-- May vary according to your needs --> 5 android:layout_marginRight="16dp" <!-- May vary according to your needs --> 6 android:layout_centerVertical="true"> 7 <!-- following imageView acts as the boarder which sitting in the background of our main container ImageView --> 8 <ImageView 9 android:layout_width="112dp" 10 android:layout_height="112dp" 11 android:background="#000"/> 12 <!-- following imageView holds the image as the container to our image --> 13 <!-- layout_margin defines the width of our boarder, here it's 1dp --> 14 <ImageView 15 android:layout_width="110dp" 16 android:layout_height="110dp" 17 android:layout_margin="1dp" 18 android:id="@+id/itemImageThumbnailImgVw" 19 android:src="@drawable/banana" 20 android:background="#FFF"/> </FrameLayout>
在下面的回答中我已经解释得很好了,请看一下!
我希望这对其他人有帮助!