Android的Elevation与TranslationZ探索

文章目录

Elevation

表示视图控件所在的高度,值越大对应的阴影越大,看起来高度越高
###TranslationZ
同样表示视图控件所在的高度,但是其表示的是相对高度

例子

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zxl.test_elevation.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:background="#3f51b5"
                android:elevation="2dp">

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">

            </LinearLayout>

        </LinearLayout>

        <TextView
            android:id="@+id/tv"
            android:layout_width="80dp"
            android:layout_height="48dp"
            android:gravity="center"
            android:layout_gravity="center"
            android:text="test"
            android:background="#448aff"
            android:elevation="6dp"/>

    </FrameLayout>

</LinearLayout>
public class MainActivity extends AppCompatActivity {

    private TextView tv;

    private boolean isSelected = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = findViewById(R.id.tv);

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!isSelected){
                    isSelected = true;
                    tv.setTranslationZ(6);
                    System.out.println("zxl--->0--->"+tv.getElevation()+"--->"+tv.getTranslationZ());
                }else{
                    isSelected = false;
                    tv.setTranslationZ(-6);
                    System.out.println("zxl--->1--->"+tv.getElevation()+"--->"+tv.getTranslationZ());
                }
            }
        });
    }
}

这里写图片描述

说明

        01-23 14:53:23.620 7029-7029/com.zxl.test_elevation I/System.out: zxl--->0--->6.0--->6.0
        01-23 14:53:30.678 7029-7029/com.zxl.test_elevation I/System.out: zxl--->1--->6.0--->-6.0

从程序运行后log可以看出
视图控件的整个高度值是Elevation+TranslationZ,当其值为0时则没有阴影了,当其值小于0在在上一个控件下方,当其值大于0则有阴影,从而出现高度。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值