1.1 创建相应的样式文件
右键点击res -> 选择Android resource file -> 设置名称styles,设置存放目录values-v21,设置支持的版本211.2 将values目录下的styles.xml文件中的代码拷贝过来,修改样式主题是android:Theme.Material
如果想要使用白色主题使用android:Theme.Material.Light <style name="AppTheme" parent="android:Theme.Material.Light"></style>1.3 手动更改相应的样式
<style name="AppTheme" parent="android:Theme.Material.Light"> <!-- Customize your theme here. --> <item name="android:colorPrimary">#FF0000</item> <item name="android:colorPrimaryDark">#00FF00</item> <item name="android:colorAccent">@color/colorAccent</item> </style>1.4 通过主题编辑页面进行操作
点击上部Open editer选项进入主题编辑页面,可以直观的查看设置的样式2.1 elevation表示view的高度,高度越大,阴影越大,可以在xml中直接使用属性,也可以在代码中使用view.setEvelvation();
海拔阴影高度属性,高度不能超过父窗体,超过父窗体的阴影部分会被截取掉,并且设置阴影高度的控件背景不能是透明的 android:elevation="30dp" 使用 <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"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:elevation="10dp" android:background="#FFFFFF" android:text="阴影"/> </RelativeLayout>2.2 通过设置elevation属性实现控件重叠效果
将图片存放到mipmap-hdpi(用来替换drawable的,它在屏幕处理的时候效率会更高)目录中 <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"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:elevation="10dp" android:background="#FFFFFF" <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/head" android:background="#FFFFFF" android:elevation="15dp" /> </RelativeLayout>2.3 TranslationZ设置
elevation是设置阴影,TranslationZ是z轴上移动的距离(可以通过TranslationX属性进行验证),因为z轴移动所以动态带有阴影 <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"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:translationZ="20dp" android:background="#FFFFFF" android:text="阴影"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/head" android:background="#FFFFFF" android:translationZ="25dp"/> </RelativeLayout>