Cast shadow on top of LinearLayout using android:elevation

前端 未结 4 973
轻奢々
轻奢々 2020-12-09 15:48

I have this LinearLayout that is going to be placed on the bottom of an activity layout. I want this LinearLayout to have a 4dp elevation, just lik

相关标签:
4条回答
  • 2020-12-09 16:15

    Add this code above of view in which you want top elevation or add android:layout_marginTop="-4dp" and add below of above view

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0.01dp"
        android:layout_below="@+id/scrollbar"
        app:cardBackgroundColor="@android:color/transparent"
        app:cardCornerRadius="0dp"
        app:cardElevation="@dimen/dp_4" />
    
    0 讨论(0)
  • 2020-12-09 16:26

    You can't theoretically do it with android:elevation, in the sense that you can't choose the direction where the shadow is going to be cast.

    There are two solutions.

    1. Drawables

    You could, for instance, put an ImageView right above your layout and set android:src="@drawable/shadow". This should be a vertical GradientDrawable defined in XML, as explained here.

    2. Workaround

    While most of the shadow is actually below the view, a subtle shadow is also above. A workaround might be using a very high value for elevation, like 40dp: the bottom part is going to be hidden due to your layout, while the top is going to be expanded and look like a common shadow.

    In either case, you do not have control over the elevation value in dp, in the sense that you can't be sure your shadow is equivalent to the one cast by android:elevation=4dp.

    0 讨论(0)
  • 2020-12-09 16:29

    use like as;

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="205dp"
        android:background="@color/white"
        android:orientation="horizontal">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="-5dp"
            android:background="@color/white"
            android:elevation="3dp"
            android:paddingTop="5dp">
    
            // CHILD VIEWS
        </RelativeLayout>
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-09 16:37

    Try this i am unable to set background through xml then i tried the programmatic way to solve that and i work perfectly refer this if facing problem on show shadow in Linear Layout

    lprofile.setBackgroundResource(R.drawable.background_with_shadow);
    

    lprofile is my ref of Linear layout and background_with_shadow is xml for applying shadow i hope i will for ur requirmement...thank u..

    0 讨论(0)
提交回复
热议问题