Android 阴影背景

1.首先看效果
Android 阴影背景_第1张图片
2.xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.youledong.sport.test.MainActivity">

    <TextView
        android:id="@+id/tv_01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/bg"
        android:gravity="center"
        android:padding="20dp"
        android:enabled="false"
        android:text="登录"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:textColor="#fff"
        android:textSize="17sp"
        android:textStyle="bold" />

    <TextView
        android:layout_marginTop="30dp"
        android:layout_below="@+id/tv_01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg"
        android:gravity="center"
        android:padding="20dp"
        android:enabled="true"
        android:text="登录"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:textColor="#fff"
        android:textSize="17sp"
        android:textStyle="bold" />
RelativeLayout>

3.用shape制作背景bg.xml


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <size android:width="42dp" android:height="42dp" />
                    <solid android:color="@android:color/transparent" />
                shape>
            item>
            <item android:gravity="center">
                <shape android:shape="rectangle">
                    <corners android:radius="30dp" />
                    <gradient android:angle="270" android:centerColor="#FEF13D" android:endColor="#FFF4DF" android:startColor="#FEF13D" />
                shape>
            item>
            <item android:bottom="6dp" android:gravity="center">
                <shape android:shape="rectangle">
                    <corners android:radius="30dp" />
                    <solid android:color="#ffc047" />
                shape>
            item>
        layer-list>
    item>

    <item android:state_enabled="false">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <size android:width="42dp" android:height="42dp" />
                    <solid android:color="@android:color/transparent" />
                shape>
            item>
            <item android:gravity="center">
                <shape android:shape="rectangle">
                    <corners android:radius="30dp" />
                    <gradient android:angle="270" android:centerColor="#D1D1D1" android:endColor="#EEEEEE" android:startColor="#FEF13D" />
                shape>
            item>
            <item android:bottom="6dp" android:gravity="center">
                <shape

                    android:shape="rectangle">
                    <corners android:radius="30dp" />
                    <solid android:color="#D1D1D1" />
                shape>
            item>
        layer-list>
    item>

selector>

当然5.0后用cardview更好实现阴影背景。

你可能感兴趣的:(笔记)