Android中的简单布局阴影

Meh*_*way 14 android shadow android-layout

所以我一直在网上搜索一种为布局显示简单阴影的方法,但没有正确的方法来做到这一点.
我找到的只是一种解决方法,你可以在你想要应用阴影的那个背后创建一个布局,然后将它调整为透明和其他一些东西.

有没有其他方法可以在不添加全新布局的情况下拥有简单的布局阴影?

Meh*_*way 46

我已经能够找到解决这个问题的方法,并View在我们着名的布局下添加一个,显示从一种颜色到另一种颜色的渐变.
通常,第一种颜色是某种深灰色,第二种颜色是背景颜色(在我的情况下,我将具有浅灰色背景,因此它不是完全白色).

布局+视图

xml会像这样:

...
<LinearLayout
    android:id="@+id/headerLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/headerImage"
        android:orientation="vertical" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="5dip"
        android:background="@drawable/drop_shadow" >
    </View>
</LinearLayout>
...
Run Code Online (Sandbox Code Playgroud)

drop_shadow.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">    
    <gradient
        android:startColor="#404040"
        android:endColor="#F1F1F1"
        android:angle="270"
        >
    </gradient>
</shape>
Run Code Online (Sandbox Code Playgroud)

我希望它会有所帮助;)


Ovo*_*eta 7

您可以使用android.support.v4.view.ViewCompat使用静态方法设置视图高程的类setElevation.该类是一个帮助程序,用于以向后兼容的方式访问API级别4之后引入的视图中的功能.

基准高程以像素为单位

View mFab = (View) findViewById(R.id.floating_button);
ViewCompat.setElevation(mFab, 12);
Run Code Online (Sandbox Code Playgroud)


Ode*_*ner 6

对于棒棒糖及以上,您可以使用高程.

对于旧版本:

这是一个懒惰的黑客来自:http: //odedhb.blogspot.com/2013/05/android-layout-shadow-without-9-patch.html

(toast_frame在KitKat上不起作用,阴影从吐司中移除)

只需使用:

android:background="@android:drawable/toast_frame"
Run Code Online (Sandbox Code Playgroud)

要么:

android:background="@android:drawable/dialog_frame"
Run Code Online (Sandbox Code Playgroud)

作为背景

例子:

<TextView
        android:layout_width="fill_parent"
        android:text="I am a simple textview with a shadow"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:padding="16dp"
        android:textColor="#fff"
        android:background="@android:drawable/toast_frame"
        />
Run Code Online (Sandbox Code Playgroud)

并且具有不同的bg颜色:

<LinearLayout
        android:layout_height="64dp"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:background="@android:drawable/toast_frame"
        android:padding="4dp"
        >
    <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Button shadow"
            android:background="#33b5e5"
            android:textSize="24sp"
            android:textStyle="bold"
            android:textColor="#fff"
            android:layout_gravity="center|bottom"
            />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)