首页 > 解决方案 > 无法将渐变设置为圆形背景或图像视图的 src

问题描述

我创建的渐变如下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="45"
        android:dither="true"
        android:endColor="#00cff7"
        android:startColor="#0095bf" />
</shape>

在我的 XML 文件中,我希望这个渐变显示为一个圆圈。

我尝试了以下方法,但渐变显示为正方形。我尝试了以下第三方库,但渐变不可见。

XML

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:src="@drawable/blue_bg"
    app:civ_border_width="2dp"
    app:civ_border_color="#FF000000"/>

尝试了以下链接,但渐变仍然是方形的。

关于如何实现这一点的任何帮助?

注意:我不希望以圆形创建渐变,因为我稍后会在应用程序中重复使用它

标签: androidxmlandroid-layoutgradientcircleimage

解决方案


像这样使用CardView

 <android.support.v7.widget.CardView
        app:cardCornerRadius="192dp"
        android:backgroundTint="@color/colorPrimary"
        android:layout_width="96dp"
        android:layout_height="96dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </LinearLayout>
    </android.support.v7.widget.CardView>

你不再需要CircleImageView了。


推荐阅读