【问题标题】:Cannot set gradient as a circle background or src to an image view无法将渐变设置为圆形背景或图像视图的 src
【发布时间】:2018-09-30 13:54:12
【问题描述】:

我创建的渐变如下

<?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 文件中,我希望这个渐变显示为一个圆圈。

我尝试了以下方法,但渐变显示为正方形。 我尝试了以下third party library,但渐变不可见。

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"/>

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

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

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

【问题讨论】:

标签: android xml android-layout gradient circleimage


【解决方案1】:

像这样使用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了。

【讨论】:

    【解决方案2】:

    你没有指定形状:

    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
       <size 
           android:width="120dp"
            android:height="120dp"/>
    </shape>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      相关资源
      最近更新 更多