Android的Elevation与TranslationZ探索

Android的Elevation与TranslationZ探索,第1张

概述Elevation表示视图控件所在的高度,值越大对应的阴影越大,看起来高度越高###TranslationZ同样表示视图控件所在的高度,但是其表示的是相对高度例子<LinearLayout xmlns:android="http://schemas.android.com/apkes/android"    xmlns:app="http://schemas.android.com/apke


Elevation

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

例子
<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则有阴影,从而出现高度。

总结

以上是内存溢出为你收集整理的Android的Elevation与TranslationZ探索全部内容,希望文章能够帮你解决Android的Elevation与TranslationZ探索所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1005107.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存