【问题标题】:(Android) How can I put a drop shadow in a Custom View?(Android) 如何在自定义视图中放置阴影?
【发布时间】:2021-12-30 05:08:59
【问题描述】:

我想使用 View 创建一个具有以下设计的自定义按钮,并像下面的设计一样给它一个阴影。1

阴影选项为:box-shadow: 10px 7px 25px 0px #0000001F

  • main_round_teamsbtn.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:padding="10dp"
            android:shape="rectangle">
            <solid android:color="@color/btn_color" />
            <corners
                android:bottomLeftRadius="5dp"
                android:bottomRightRadius="5dp"
                android:topLeftRadius="5dp"
                android:topRightRadius="105dp" />
        </shape>
    </item>
</layer-list>

  • teams_main_btn.xml
<?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="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/main_round_teamsbtn"

    android:paddingStart="16dp">

    <TextView
        android:id="@+id/teamsHead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/notosans_bold"
        android:includeFontPadding="false"
        android:text="원하는\n공모전을\n찾아보세요"
        android:textColor="@color/black"
        android:textSize="22sp"
        app:layout_constraintBottom_toTopOf="@+id/teamsSub" />

    <TextView
        android:id="@+id/teamsSub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/notosans_regular"
        android:text="현재 구하고 있는 팀 둘러보기"
        android:textColor="#8C8C8C"
        android:textSize="10sp"
        app:layout_constraintBottom_toBottomOf="parent" />

    <ImageView
        android:id="@+id/teamsImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_marginEnd="4dp"
        android:background="@drawable/findteams_btn"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0" />


</androidx.constraintlayout.widget.ConstraintLayout>
  • main.xml
...
<com.example.moizar.MainTeamsBtnView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
...

我想要一个如何给阴影的答案

【问题讨论】:

标签: android kotlin button view shadow


【解决方案1】:
  • teams_main_btn.xml

您可以在 TextView 或 ImageView 中使用属性elevation

android:elevation="10dp"

您可以观看此链接了解更多信息:

https://www.youtube.com/watch?v=nNHChjTZCtw&ab_channel=CodeAndDesign

【讨论】:

    【解决方案2】:

    我怀疑你是否可以通过代码实现你想要的,即使这可能你会编写很多代码,可能涉及RenderScript 或 Android 12 上的一些新 API...

    考虑将您的图片用作9-patch drawable,并正确标记内容/拉伸区域。内容(文本?)将是全高和半宽,仅拉伸区域的第一个像素

    【讨论】:

      猜你喜欢
      • 2020-09-20
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      相关资源
      最近更新 更多