Android给布局、控件加阴影效果的示例代码

时间:2022-07-28
本文章向大家介绍Android给布局、控件加阴影效果的示例代码,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

增加阴影效果,让控件或者布局看起来有立体的效果,总的来说有两种解决方案。

1,直接使用属性: android:elevation=”4dp”这样一句代码,就实现了效果,elevation表示海拔,就是布局的z轴的高度,调整高度,可以选择阴影的轻重。

<TextView 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:elevation="4dp"
        android:background="@drawable/home_waitcourse_yellow_shape"
        android:textColor="@color/foorYellow"  
        android:text="报道"/ 

2,这种方式就需要写点代码了,但是也不多,是通过写一个xml来实现的。

<?xml version="1.0" encoding="utf-8"?  
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"   

  <!-- 阴影部分 --  
  <!-- 个人觉得更形象的表达:top代表下边的阴影高度,left代表右边的阴影宽度。其实也就是相对应的offset,solid中的颜色是阴影的颜色,也可以设置角度等等 --  
  <item 
    android:left="2dp" 
    android:top="2dp" 
    android:right="2dp" 
    android:bottom="2dp"  
    <shape android:shape="rectangle"   

      <gradient 
        android:angle="270" 
        android:endColor="#0F000000" 
        android:startColor="#0F000000" /  

      <corners 
        android:bottomLeftRadius="6dip" 
        android:bottomRightRadius="6dip" 
        android:topLeftRadius="6dip" 
        android:topRightRadius="6dip" /  
    </shape  
  </item  

  <!-- 背景部分 --  
  <!-- 形象的表达:bottom代表背景部分在上边缘超出阴影的高度,right代表背景部分在左边超出阴影的宽度(相对应的offset) --  
  <item 
    android:left="3dp" 
    android:top="3dp" 
    android:right="3dp" 
    android:bottom="5dp"  
    <shape android:shape="rectangle"   

      <gradient 
        android:angle="270" 
        android:endColor="#FFFFFF" 
        android:startColor="#FFFFFF" /  

      <corners 
        android:bottomLeftRadius="6dip" 
        android:bottomRightRadius="6dip" 
        android:topLeftRadius="6dip" 
        android:topRightRadius="6dip" /  
    </shape  
  </item  
</layer-list  

以上就是本文的全部内容,希望对大家的学习有所帮助。