如何在约束布局项上添加阴影?
How to add shadow on constraint layout items?
我想在约束布局中为注释添加阴影。我尝试过使用提升,但这似乎不起作用。此外,如果我尝试使用单独的文件来设置背景样式,如果我应用它,便笺的背景颜色也会发生变化。这个应用程序允许在创建笔记时指定它的颜色,我想保持这种方式。但正如我所提到的,如果我从我的可绘制对象中指定背景,则笔记的背景颜色也会发生变化。我也尝试过使用卡片视图,但它只是覆盖了项目而没有出现。这是我拥有的:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/row_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:maxLength="20"
android:text="Title"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/notes_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:maxLength="350"
android:maxLines="12"
android:text="Example text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/title_text"
app:layout_constraintTop_toBottomOf="@+id/title_text"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
notes
我希望笔记有这样的阴影:
goal
我怎样才能做到这一点?
为了在您的布局中获得阴影,您必须使用 CardView 布局,在约束布局之上或之下,具体取决于您如何配置 XML 布局。您可以从 androidx.cardview:cardview:1.0.0 依赖项中使用它。它具有生成阴影的高度属性。
请参阅 GFG and also this 上的教程。
您还可以在 CardView Based layout and cardview widget
阅读官方文档
这里是一个在 cardView 布局中使用阴影的例子
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Card Example"
android:layout_marginTop="50dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:textColor="@color/LightBlack"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3">
<Spinner
android:id="@+id/locationSpinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="25sp" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
看图好像是
我打赌这就是您要搜索的内容:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
app:cardCornerRadius="8dp"
android:layout_margin="10dp"
app:cardElevation="8dp"
tools:ignore="MissingConstraints">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="Title"
android:textColor="#000000"
android:layout_margin="16dp"/>
<TextView
android:id="@+id/notes_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Example Text"
android:layout_margin="16dp"
android:textColor="#000000"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints"
android:layout_below="@+id/title_text"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
在 CardView
中使用 RelativeLayout
也很重要,这里是在 CardView
和 TextView
之间(就像我所做的那样)。
快乐的编码伙伴! :)
我想在约束布局中为注释添加阴影。我尝试过使用提升,但这似乎不起作用。此外,如果我尝试使用单独的文件来设置背景样式,如果我应用它,便笺的背景颜色也会发生变化。这个应用程序允许在创建笔记时指定它的颜色,我想保持这种方式。但正如我所提到的,如果我从我的可绘制对象中指定背景,则笔记的背景颜色也会发生变化。我也尝试过使用卡片视图,但它只是覆盖了项目而没有出现。这是我拥有的:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/row_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:maxLength="20"
android:text="Title"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/notes_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:maxLength="350"
android:maxLines="12"
android:text="Example text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/title_text"
app:layout_constraintTop_toBottomOf="@+id/title_text"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
notes
我希望笔记有这样的阴影: goal
我怎样才能做到这一点?
为了在您的布局中获得阴影,您必须使用 CardView 布局,在约束布局之上或之下,具体取决于您如何配置 XML 布局。您可以从 androidx.cardview:cardview:1.0.0 依赖项中使用它。它具有生成阴影的高度属性。
请参阅 GFG and also this 上的教程。 您还可以在 CardView Based layout and cardview widget
阅读官方文档这里是一个在 cardView 布局中使用阴影的例子
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Card Example"
android:layout_marginTop="50dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:textColor="@color/LightBlack"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3">
<Spinner
android:id="@+id/locationSpinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="25sp" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
看图好像是
我打赌这就是您要搜索的内容:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
app:cardCornerRadius="8dp"
android:layout_margin="10dp"
app:cardElevation="8dp"
tools:ignore="MissingConstraints">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="Title"
android:textColor="#000000"
android:layout_margin="16dp"/>
<TextView
android:id="@+id/notes_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Example Text"
android:layout_margin="16dp"
android:textColor="#000000"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints"
android:layout_below="@+id/title_text"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
在 CardView
中使用 RelativeLayout
也很重要,这里是在 CardView
和 TextView
之间(就像我所做的那样)。
快乐的编码伙伴! :)