如何在Android中为ImageView设置边框并更改其颜色?


当前回答

你可以在Android Studio中使用9个补丁来制作边框!

我一直在寻找解决方案,但我没有找到,所以我跳过了这部分。

然后我去Firebase资产的谷歌图像,我偶然发现他们使用9patch。

这是链接:https://developer.android.com/studio/write/draw9patch

你只需要拖动边缘所在的位置。

这就像Unity中的border edge。

其他回答

只需在ImageView中添加以下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

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

    <size
        android:width="20dp"
        android:height="20dp"/>
    <stroke
        android:width="4dp" android:color="@android:color/black"/>
    <padding android:left="1dp" android:top="1dp" android:right="1dp"
        android:bottom="1dp" />
</shape>

首先添加你想要的背景色作为你的边界的颜色,然后

更改cropToPadding为true,然后添加填充。

然后你就有了imageView的边框。

你可以在Android Studio中使用9个补丁来制作边框!

我一直在寻找解决方案,但我没有找到,所以我跳过了这部分。

然后我去Firebase资产的谷歌图像,我偶然发现他们使用9patch。

这是链接:https://developer.android.com/studio/write/draw9patch

你只需要拖动边缘所在的位置。

这就像Unity中的border edge。

你必须在res/drawable这段代码中创建一个background.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="6dp" />
<stroke
    android:width="6dp"
    android:color="@android:color/white" />
<padding
    android:bottom="6dp"
    android:left="6dp"
    android:right="6dp"
    android:top="6dp" />
</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>

在下面的答案中,我已经解释得很好了,请大家也看看!

我希望这将有助于其他人在那里!