how to set shadow to a View in android?

后端 未结 6 1263
醉话见心
醉话见心 2020-11-27 03:24

I want to know how to add a shadow layer to any general View in android. for eg: suppose i have a layout xml, showing something like this..



        
相关标签:
6条回答
  • 2020-11-27 04:10

    Here's my cheesy version of the solution...This is the modification of the solution found here

    I didn't like how the corners look so I faded all of them...

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Layer 0-->
    <!--Layer 1-->
    <!--Layer 2-->
    <!--Layer 3-->
    <!--Layer 4 (content background)-->
    
    <!-- dropshadow -->
    <item>
        <shape>
            <gradient 
                android:startColor="@color/white"
                android:endColor="@color/white"
                android:centerColor="#10CCCCCC"
                android:angle="180"/>
            <padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
        </shape>
    </item>
    
    <item>
        <shape>
            <gradient 
                android:startColor="@color/white"
                android:endColor="@color/white"
                android:centerColor="#20CCCCCC"
                android:angle="180"/>
            <padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
        </shape>
    </item>
    
    <item>
        <shape>
            <gradient 
                android:startColor="@color/white"
                android:endColor="@color/white"
                android:centerColor="#30CCCCCC"
                android:angle="180"/>
    
            <padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
        </shape>
    </item>
    
    <item>
        <shape>
            <gradient 
                android:startColor="@color/white"
                android:endColor="@color/white"
                android:centerColor="#40CCCCCC"
                android:angle="180"/>
            <padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
        </shape>
    </item>
    
    <item>
        <shape>
            <gradient 
                android:startColor="@color/white"
                android:endColor="@color/white"
                android:centerColor="#50CCCCCC"
                android:angle="180"/>
            <padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
        </shape>
    </item>
    
    <!-- content background -->
    <item>
        <shape>
            <solid android:color="@color/PostIt_yellow" />
        </shape>
    </item>
    

    0 讨论(0)
  • 2020-11-27 04:12

    You can use elevation, available since API level 21

    The elevation of a view, represented by the Z property, determines the visual appearance of its shadow: views with higher Z values cast larger, softer shadows. Views with higher Z values occlude views with lower Z values; however, the Z value of a view does not affect the view's size. To set the elevation of a view:

    in a layout definition, use the  

    android:elevation
    

     attribute. To set the elevation of a view in the code of an activity, use the

    View.setElevation()
    

     method.

    Source

    0 讨论(0)
  • 2020-11-27 04:20

    The best way to create a shadow is to use a 9patch image as the background of the view (or a ViewGroup that wraps the view).

    The first step is to create a png image with a shadow around it. I used photoshop to create such an image. Its really simple.

    • Create a new image with Photoshop.
    • Add a layer and create a black square of 4x4.
    • Create a shadow on the layer by selecting the layer in layer explorer and clicking on a button titled fx and choosing drop shadow.
    • Export the image as png.

    The next step is to create 9-patch drawables from this image.

    • Open draw9patch from android-sdk/tools
    • Open the image in draw9patch
    • Create 4 black lines on the four sides of the square like the following and then save the image as shadow.9.png.

    Now you can add this shadow as the background of the views you want to add the shadow to. Add shadow.9.png to res/drawables. Now add it as a background:

    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/shadow"
      android:paddingBottom="5px"
      android:paddingLeft="6px"
      android:paddingRight="5px"
      android:paddingTop="5px"
    >
    

    I recently wrote a blog post that explains this in detail and includes the 9patch image that I use for creating the shadow.

    0 讨论(0)
  • 2020-11-27 04:25

    </LinearLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="@color/dropShadow" />
    

    Use Just Below the LinearLayout

    Another Method

    create "rounded_corner_bg.xml" in /drawable folder

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/primaryColor" />
    
            <corners android:radius="4dp" />
        </shape>
    </item>
    
    <item
        android:bottom="2dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#F7F7F7" />
    
            <corners android:radius="4dp" />
        </shape>
    </item>
    
    </layer-list>
    

    To use this Layout android:background="@drawable/rounded_corner_bg"

    0 讨论(0)
  • 2020-11-27 04:29

    Assuming u would use a linear layout(i have considered a vertical linear layout)..and have a view just below your linear layout.Now for this view provide a start colour and end colour.. I also wanted to get this thing,its working for me..If you need a even better effect,then just work around the start and end colour.

    activity_main

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:id="@+id/vertical"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:background="@drawable/layout_back_bgn"
            android:orientation="vertical" >
        </LinearLayout>
    
        <View
            android:layout_below="@+id/vertical"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="@drawable/shadow"
            >
        </View>
    
    
    </LinearLayout>
    

    layout_back_bgn.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    
    <solid android:color="#FF4500" />
    
    </shape>
    

    shadow.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">    
        <gradient
            android:startColor="#4D4D4D"
            android:endColor="#E6E6E6"
            android:angle="270"
            >
        </gradient>
    </shape>
    

    I tried to post an image which i have it after using the above code,but stackoverflow doesnot allow me coz i dont have reputation..Sorry about that.

    0 讨论(0)
  • 2020-11-27 04:30

    There are a simple trick, using two views that form the shadow.

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:padding="10dp"
        android:background="#CC55CC">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="0">
                <TableRow>
                    <LinearLayout
                        android:id="@+id/content"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                        <TextView  
                            android:layout_width="fill_parent" 
                            android:layout_height="wrap_content"
                            android:background="#FFFFFF" 
                            android:text="@string/hello" />
                    </LinearLayout>
                    <View
                        android:layout_width="5dp"
                        android:layout_height="fill_parent"
                        android:layout_marginTop="5dp"
                        android:background="#55000000"/>
                </TableRow>
            </TableLayout>
            <View
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:layout_marginLeft="5dp"
                android:background="#55000000"/>
        </LinearLayout>
    </FrameLayout>
    

    Hope this help.

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