在android中透明imageView的边框

时间:2012-10-10 13:05:00

标签: android imageview border

我想要一个只有边框并且内部透明的图像视图。获取边框的常见技巧似乎是在我们想要边框的imageView下面使用稍大一些尺寸略大的图像视图但这不会在这里工作,因为我想要一个透明的imageview。 我该如何创建它?

2 个答案:

答案 0 :(得分:11)

在drawable文件夹中创建一个新的backgroundcolor.xml文件

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@color/transparent" />

    <stroke
        android:width="0.5dip"
        android:color="@color/black" />


    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />

</shape>

并将其添加为您的imageview的背景

答案 1 :(得分:0)

您可以将xml文件用作imageview的drawable。例如,将此xml文件作为border.xml放在drawable-mdpi文件夹中:

<?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>

然后在主布局中,将其用作imageview的可绘制背景。 imageview将为空,仅显示边框。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="100dp"
        android:src="@drawable/border" />

</LinearLayout>