朋友们如何在ImageView中显示边框?
我希望结果像手机相册那样,所有图片都带有边框。
请告诉我答案,提前感谢您...
朋友们如何在ImageView中显示边框?
我希望结果像手机相册那样,所有图片都带有边框。
请告诉我答案,提前感谢您...
ImageView
的“边框”(实际上是背景)创建一个资源(图层可绘制的XML),并在您的theme
中声明ImageView
的背景资源为该可绘制的XML。ImageView
的状态(focused
,selected
等)更改此“边框”,则应创建更多的图层可绘制对象,并将它们组合成选择器XML(状态可绘制对象)。然后在主题中,您应将ImageView
的背景设置为这个selector.xml
。
更新
以下是如何指定简单边框的示例,结果如下所示:
您需要执行以下操作:image_border.xml
),
2. 修改/创建您的styles.xml
文件
3. 修改/创建你的colors.xml
文件
4. 修改您的布局 XML 文件(或您的代码),以将样式应用于 ImageView
。
res/drawable/image_border.xml<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient android:angle="90"
android:startColor="@color/image_border_start"
android:centerColor="@color/image_border_center"
android:endColor="@color/image_border_end" />
</shape>
</item>
<item android:top="2dp" android:left="2dp"
android:right="2dp" android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="@color/default_back_color" />
</shape>
</item>
</layer-list>
res/values/styles.xml
添加以下行:
<style name="myImageView">
<!-- 3dp so the background border to be visible -->
<item name="android:padding">3dp</item>
<item name="android:background">@drawable/image_border</item>
<item name="android:scaleType">fitCenter</item>
</style>
res/values/colors.xml
添加以下行:
<color name="image_border_start">#40990000</color>
<color name="image_border_end">#FF660000</color>
<color name="image_border_center">#FFFF3333</color>
最后,在布局XML中指定ImageView
的样式:
<ImageView android:id="@+id/my_image"
android:layout_width="100dp" android:layout_height="100dp"
android:src="@drawable/efteling"
style="@style/myImageView" />
android:background="@drawable/以下代码文件名称"
。<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</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>