【问题标题】:Creating Android Shape with rounded corners and shadow and custom image创建具有圆角和阴影以及自定义图像的 Android 形状
【发布时间】:2018-11-09 19:00:40
【问题描述】:

我正在尝试为ImageView 创建一个shape,它有圆角、实际的image 和一个阴影。当我创建 shape 并设置以下变量时,我得到了一个带有阴影的圆形图像:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#42000000" />
    <corners android:radius="5dp" />
</shape>

现在,当我尝试添加自定义图像时(就像许多人建议我使用 Layer-list 一样,我得到了图像,但圆角和阴影消失了:

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

    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#42000000" />
            <corners android:radius="5dp" />
        </shape>
    </item>

    <item android:drawable="@drawable/no_cover_art" />
</layer-list>

我还将以编程方式更改“图像”,所以也许这也是需要提及的事情。

我做错了什么,如何获得带有阴影和自定义图像的圆角?

编辑 我曾尝试仅将形状用作可绘制对象,但我得到的结果是将角应用于其后面的东西并且不会更改 ImageView 本身。

<ImageView
android:id="@+id/song_album_cover"
android:layout_width="64dp"
android:layout_height="68dp"
android:layout_marginStart="8dp"
android:layout_marginTop="6dp"
android:elevation="18dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/no_cover_art"
android:background="@drawable/artist_cover_image"/>

左侧是Android Studio 向我展示的图片,右侧是我实际运行应用程序时的图片。

【问题讨论】:

    标签: android xml drawable shapes


    【解决方案1】:

    使用 drawable 作为图像视图的背景,然后将 src 设置为您想要的任何图像。

    【讨论】:

    • 当使用 shape 作为可绘制对象和使用 app:srcCompat=" 的图像时,它似乎会覆盖它并且不成形
    【解决方案2】:

    /使用需要在imageview中使用下面这个东西,把你的代码放到drawable文件中,然后按照下面的方式添加/ android:background="@drawable/nameofdrawablefile"

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 1970-01-01
      • 2014-10-24
      • 2014-02-19
      • 2023-04-01
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多