朋友们如何在Imageview中显示边框?
我想要像移动画廊一样的结果,所有图像都带有边框。
请给我谢谢你的提前....
朋友们如何在Imageview中显示边框?
我想要像移动画廊一样的结果,所有图像都带有边框。
请给我谢谢你的提前....
ImageView
您可以为您的“边框”(实际上是背景)创建一个资源(图层可绘制 xml) ,并在您的声明中声明theme
该ImageView
背景资源是可绘制 xml。
如果您需要根据ImageView
的状态(focused
、selected
等)更改此“边框”,那么您应该创建更多图层可绘制对象,并将它们一起放入选择器 xml(可绘制状态)。
然后在你的主题中你应该将ImageView
背景设置为这个selector.xml
。
更新
下面是如何为图像指定简单边框的示例,这将导致
你必须
image_border.xml
),styles.xml
文件colors.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>
ImageView
最后在布局 xml中指定您的样式:
<ImageView android:id="@+id/my_image"
android:layout_width="100dp" android:layout_height="100dp"
android:src="@drawable/efteling"
style="@style/myImageView" />
您可以将此 XML 文件用作可绘制文件而不是多个文件,但此文件放置在可绘制文件夹中。在 ImageView 中,将此 XML 文件用作背景可绘制对象,例如:android:background="@drawable/following code file
name"
.
我希望这对你有帮助。
<?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>
我尝试了上述所有解决方案,但它们对我不起作用!所以我想出了一个简单的解决方案!:-)
我记得我在下面的文章中读到了 Android 的 FrameLayout ,它说它可以帮助我们按照我们添加它们的顺序将我们的 UI 元素堆叠在一起。
解决方案:
<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>
预览:
就是这样。你会得到以下类似 imageView!
优点和缺点:
我认为这是在其他任何地方使用的非常简单的解决方案,而且您所做的所有事情都放在一个地方,因此更容易修改它。但是我不喜欢添加两个 ImageView。