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