【问题标题】:Add shadow to bottom of view在视图底部添加阴影
【发布时间】:2020-04-21 13:47:55
【问题描述】:

我在这个布局上遇到了很多麻烦。

基本上我有 2 条直线,一条略带灰色,底部有阴影,另一条直线下方为白色。我的麻烦是将阴影添加到第一个线性的末尾。我尝试了“高程”,但这就是我现在需要的,因为如果最后 10dp 来自不同的颜色,我希望阴影在线性内,但我没有做对。

到目前为止,这是我的布局,我想在第一个线性视图中放置一个视图,但效果不佳。

任何想法如何做到这一点?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:background="#f8f8f8"
        android:layout_height="0dp">

        <android.support.v7.widget.CardView
            app:cardCornerRadius="10dp"
            app:cardElevation="10dp"
            android:layout_width="312dp"
            android:layout_height="204dp"
            app:cardBackgroundColor="@android:color/holo_red_light"/> 
    </LinearLayout>


    <LinearLayout
        android:background="#ffffff"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </LinearLayout>
</LinearLayout>

【问题讨论】:

标签: android android-layout


【解决方案1】:

尝试在您的cardview 所在的linearlayout 中使用下面的行。

android:elevation="8dp"

cardView 中的cardElevation 部分保持原样。

card_view:cardElevation="10dp"
card_view:cardPreventCornerOverlap="false"

也只是一个建议:

如果您在两个孩子linearLayout 中都使用android:layout_weight,请将android:weightSum="2" 放在您的父母linearLayout 中,如@Ming Leung here 给出的答案中所述

有关android:weightSum="2"的更多信息,您可以参考page上的答案

这个page 将在cardview 中为elevation 提供帮助

希望对您有所帮助。

【讨论】:

  • 我可能会导致一些混乱,所以我会编辑图像,我的卡片视图很好,问题是卡片视图所在的线性阴影。
  • 嗨 @Hugo 查看我的更新答案,如果您有任何其他疑问,请告诉我。
【解决方案2】:

我能够使用 ConstraintLayout 让它工作:

<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="match_parent">

    <androidx.cardview.widget.CardView
        android:layout_width="312dp"
        android:layout_height="204dp"
        app:cardBackgroundColor="@android:color/holo_red_light"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2017-04-14
    • 2019-11-06
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    相关资源
    最近更新 更多