美文网首页
Android elevation 用途

Android elevation 用途

作者: __素颜__ | 来源:发表于2018-09-20 16:04 被阅读534次

elevation是2014 年Google 推出了Material Design新的 Android 支持库 v7之后才有的产物,目前我就用它做阴影,但是它还有一个权重的问题。

一.添加view阴影

1.代码设置android:elevation="Xdp"

 <LinearLayout
            android:elevation="8dp"
            android:id="@+id/ll_order"
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:orientation="horizontal"
            android:background="@color/white"
 >

            <TextView
                android:id="@+id/tv_order_go"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/bt_corners_red"
                android:gravity="center"
                android:text="预约"
                android:textColor="@color/white"
                android:textSize="17sp" />
        </LinearLayout>

2.效果图:


image.png

3.Button设置没效果:
Button设置elevation阴影没效果是因为默认主题里,已经有了elevation设置,所以再设置就没用了,可以先用android:stateListAnimator=”@null”把默认动画置空,然后再设置elevation。

4.注意设置背景颜色
要设置背景颜色,没有背景颜色是没有阴影效果的,设置完后运行效果如下图。

二.设置权重

1.例如:我们再RelativeLayout/FrameLayout上盖两层view

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#e91010" />

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="40dp"
            android:background="#51df0e" />
    </RelativeLayout>

正常显示:


image.png

但是如果我们再红色布局上设置了elevation

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <View
            android:elevation="1dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#e91010" />

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="40dp"
            android:background="#51df0e" />
    </RelativeLayout>

效果是这样:


image.png

红色布局提到了最上面,android:elevation="1dp",相当于在这些View里面他的权重提高了,升官啦!

在android的世界里面,elevation起到了权重的作用,而且,每一个View的默认权重都是0dp,如果我们把红色view的elevation设置为android:elevation="0dp",那么一切照旧。

相关文章

网友评论

      本文标题:Android elevation 用途

      本文链接:https://www.haomeiwen.com/subject/gchinftx.html