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