简体   繁体   English

在视图底部添加阴影

[英]Add shadow to bottom of view

I've been having a lot of trouble with this layout.我在这个布局上遇到了很多麻烦。

Basically I have 2 linears, one that is slightly grey with that shadow on bottom and below another linear just white.基本上我有 2 条线状线,一条线略带灰色,底部有阴影,另一条线线下方只有白色。 My trouble here is adding that shadow to the end of the first linear.我的麻烦是将阴影添加到第一个线性的末尾。 I tried "elevation" but that is now what i need since i want the shadow to be inside the linear kinda if the last 10dp are from a different color but I'm not getting it right.我尝试了“海拔”,但这就是我现在需要的,因为如果最后 10dp 来自不同的颜色,我希望阴影在线性范围内,但我没有做对。

在此处输入图片说明

This is my layout so far, i thought of putting a view inside the first linear but its not working that good.到目前为止,这是我的布局,我想在第一个线性内部放置一个视图,但效果不佳。

Any ideas how to do this?任何想法如何做到这一点?

<?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>

Try using below line in the linearlayout under which your cardview is sitting.尝试在使用以下行linearlayout下,你的cardview坐着。

android:elevation="8dp"

Let the cardElevation part in your cardView be as it is.cardElevation中的cardElevation部分保持cardView

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

Also just a suggestion :也只是一个建议:

If you're using android:layout_weight in both child linearLayout put android:weightSum="2" in your parent linearLayout as mentioned in answer given by @Ming Leung here如果您在两个子linearLayout使用android:layout_weightandroid:weightSum="2"放在您的父linearLayout如@Ming Leung here给出的答案中所述

For more information on android:weightSum="2" you can refer to answers on this page有关android:weightSum="2"更多信息,您可以参考此页面上的答案

This page would help you for elevation in cardview页面将帮助你elevationcardview

Hope that helps.希望有帮助。

I was able to get it to work using a ConstraintLayout:我能够使用 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>

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM