如何在 Imageview 中显示边框? [英] How To Display Border To Imageview?

查看:40
本文介绍了如何在 Imageview 中显示边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友如何在Imageview中显示边框?

Friends How To Display Border To Imageview ?

我想要像移动图库一样显示所有带有边框的图像.

I Want To Result Like Mobile gallery all image display with border.

请给我答案,感谢您的提前....

plz give me ans thanks for advance....

推荐答案

你可以为你的 ImageView 的边框"(实际上是背景)创建一个资源(layer drawable xml),并在你的 theme ImageView 的背景资源是可绘制的 xml.

You can create a resource (layer drawable xml) for your ImageView's "border" (actually background), and declare in your theme that the ImageView's background resource is the drawable xml.

如果您需要根据 ImageView 的状态(focusedselected 等)更改此边框",那么您应该创建更多图层可绘制对象,并将它们放在一个选择器 xml(状态可绘制对象)中.
然后在您的主题中,您应该将 ImageView 的背景设置为此 selector.xml.

If you need this "border" to be changed based on the ImageView's state (focused, selected, etc.), then you should create more layer drawables, and put them together into a selector xml (state drawable).
Then in your theme you should set the ImageView's background to be this selector.xml.

更新
下面是如何为图像指定简单边框的示例,这将导致

Update
Below is a sample of how to specify a simple border to your images, that will result in

你必须

  1. 创建一个新的图层可绘制文件(image_border.xml),
  2. 修改/创建您的styles.xml 文件
  3. 修改/创建您的colors.xml 文件
  4. 修改您的布局 xml 文件(或您的代码)以将样式应用到 ImageView.
  1. create a new layer drawable file (image_border.xml),
  2. modify/create your styles.xml file
  3. modify/create your colors.xml file
  4. modify your layout xml file (or your code) to apply the style to the 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
添加以下几行:

res/values/styles.xml
Add the following lines:

<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
添加以下几行:

res/values/colors.xml
Add the following lines:

<color name="image_border_start">#40990000</color>
<color name="image_border_end">#FF660000</color>
<color name="image_border_center">#FFFF3333</color>

最后在 layout xml 中指定 ImageView 的样式:

And finally specify the style of your ImageView in your layout xml:

<ImageView android:id="@+id/my_image" 
    android:layout_width="100dp" android:layout_height="100dp"
    android:src="@drawable/efteling"
    style="@style/myImageView" />

这篇关于如何在 Imageview 中显示边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆