如何爲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>
在下面的回答中我已經解釋得很好了,請看一下!
我但願這對其餘人有幫助!