Android中透明ImageView的边框

4

我想要一个只有边框且内部透明的ImageView。通常获取边框的技巧似乎是在我们想要边框的imageView下方使用稍大的另一个imageView,但是这种方法在这里不起作用,因为我想要一个透明的imageView。我该如何创建它?

2个回答

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中


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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接