How to show shadow around the linearlayout in Android?

前端 未结 13 2355
名媛妹妹
名媛妹妹 2020-11-27 10:51

How can I show shadow for my linear layout. I want white colored rounded background with shadow around the linearlayout. I have done this so far.



        
相关标签:
13条回答
  • 2020-11-27 11:13

    One possible solution is using nine patch image like this http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

    OR

    I have done this in the following way. This is my main layout in which round_corner.xml and drop_shadow.xml used as background resource. round_corner_two is same like round_corner.xml only the color attribute is different. copy the round_corner.xml,drop_shadow.xml and round_conere_two.xml into drawable folder.

    <RelativeLayout
        android:id="@+id/facebook_id"
        android:layout_width="250dp"
        android:layout_height="52dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:background="@drawable/round_corner" >
    
        <LinearLayout
            android:id="@+id/shadow_id"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_margin="1dp"
            android:background="@drawable/drop_shadow" >
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_marginBottom="2dp"
                android:background="@drawable/round_corner_two"
                android:gravity="center"
                android:text="@string/fb_butn_text"
                android:textColor="@color/white" >
            </TextView>
        </LinearLayout>
    </RelativeLayout>
    

    round_corner.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    
    <!-- view background color -->
    <solid
        android:color="#ffffff" >
    </solid>
    
    <!-- view border color and width -->
    <stroke
        android:width="0dp"
        android:color="#3b5998" >
    </stroke>
    
    <!-- If you want to add some padding -->
    <padding
        android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp"    >
    </padding>
    
    <!-- Here is the corner radius -->
    <corners
        android:radius="10dp"   >
    </corners>
    
    </shape>
    

    drop_shadow.xml

    <?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="@android:color/darker_gray" />
        <corners android:radius="12dp"/>
        </shape>
    </item>
    <item android:right="1dp" android:left="1dp" android:bottom="5dp">
        <shape 
            android:shape="rectangle">
        <solid android:color="@android:color/white"/>
        <corners android:radius="5dp"/>
        </shape>
    </item>
    </layer-list>
    
    0 讨论(0)
  • 2020-11-27 11:16

    Try this.. layout_shadow.xml

    <?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="#CABBBBBB"/>
                <corners android:radius="2dp" />
            </shape>
        </item>
    
        <item
            android:left="0dp"
            android:right="0dp"
            android:top="0dp"
            android:bottom="2dp">
            <shape android:shape="rectangle">
                <solid android:color="@android:color/white"/>
                <corners android:radius="2dp" />
            </shape>
        </item>
    </layer-list>
    

    Apply to your layout like this

     android:background="@drawable/layout_shadow"
    
    0 讨论(0)
  • 2020-11-27 11:16
    1. save this 9.png. (change name it to 9.png)

    enter image description here

    2.save it in your drawable.

    3.set it to your layout.

    4.set padding.

    For example :

    <LinearLayout  
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/shadow"
      android:paddingBottom="6dp"
      android:paddingLeft="5dp"
      android:paddingRight="5dp"
      android:paddingTop="6dp"
    >
    
    .
    .
    .
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-27 11:19

    There is no such attribute in Android, to show a shadow. But possible ways to do it are:

    1. Add a plain LinearLayout with grey color, over which add your actual layout, with margin at bottom and right equal to 1 or 2 dp

    2. Have a 9-patch image with a shadow and set it as the background to your Linear layout

    0 讨论(0)
  • 2020-11-27 11:22

    There is also another solution to the problem by implementing a layer-list that will act as the background for the LinearLayoout.

    Add background_with_shadow.xml file to res/drawable. Containing:

    <?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="@android:color/darker_gray" />
            <corners android:radius="5dp"/>
            </shape>
        </item>
        <item android:right="1dp" android:left="1dp" android:bottom="2dp">
            <shape 
                android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="5dp"/>
            </shape>
        </item>
    </layer-list>
    

    Then add the the layer-list as background in your LinearLayout.

    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@drawable/background_with_shadow"/>
    
    0 讨论(0)
  • 2020-11-27 11:25

    Create a new XML by example named "shadow.xml" at DRAWABLE with the following code (you can modify it or find another better):

    <?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/middle_grey"/>
            </shape>
        </item>
    
        <item android:left="2dp"
              android:right="2dp"
              android:bottom="2dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/white"/>
            </shape>
        </item>
    
    </layer-list>
    

    After creating the XML in the LinearLayout or another Widget you want to create shade, you use the BACKGROUND property to see the efect. It would be something like :

    <LinearLayout
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:paddingRight="@dimen/margin_med"
        android:background="@drawable/shadow"
        android:minHeight="?attr/actionBarSize"
        android:gravity="center_vertical">
    
    0 讨论(0)
提交回复
热议问题