【问题标题】:How to give drop shadow in layouts in android如何在android中的布局中给出阴影
【发布时间】:2021-12-23 20:05:51
【问题描述】:

我想在我的布局中实现这个投影。我的布局是相对布局和线性布局。 我已经尝试过,但没有任何进展。 我附上了一张图片,以便帮助我实现我想要实现的目标

我的.xml


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

  <solid
      android:color="#4D0B1020"/>
  <stroke
      android:color="#1AFFFFFF"
      android:width="2dp"

      />
  <corners
      android:radius="12dp"/>

</shape>

【问题讨论】:

  • 如果不是透明背景,那么使用 cardview 作为父级就可以了。我建议的最好方法是使用阴影作为背景的 9patch 图像。你可以在 android 中做,但会有点重stackoverflow.com/a/41478599/6478047
  • 卡片视图或海拔

标签: android android-xml


【解决方案1】:

试试下面的代码:

(在这种情况下,MaterialCardView 的子元素是普通的 LinearLayout,请随意将其替换为您的自定义布局。)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardElevation="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/dropShadowLinearLayout"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:orientation="horizontal" />
    </com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>

这里的高程只有 10dp,您可以将其更改为您想要的任何值并根据自己的喜好进行自定义。

图片:


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多