Android中自定义漂亮动态样式

Android中自定义漂亮动态样式

下面给大家演示制作工款精美的控件样式,可以应用到多种控件上。

1、在res中新建个drawable文件夹,然后在这文件夹上右击选择“新建”-“其它”-“Android Xml File”-“下一步”-输入文件名“my_style”,然后在下面选择“selector”。然后打开这个Xml文件,里面添加为如下:

  

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item>
         <shape>
             <corners android:radius="10px"/>
             <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f"></gradient>            
             <stroke android:width="2px" android:color="#ff0"></stroke>
         </shape>
     </item>
     <item android:state_active="true">
         <shape>
             <corners android:radius="5px"/>
             <gradient android:startColor="#000" android:centerColor="#0f0" android:endColor="#fff"></gradient>            
             <stroke android:width="2px" android:color="#f0f"></stroke>
         </shape>
     </item>
 </selector>

  说明:纷色字体内容为您手动添加的。第一个item的内容表示默认样式,第二个item由于标注了:android:state_active="true",所以表示激活状态下的样式。根局自己的爱好,可以手动设计更多的样式,更多的item。

2、在layout布局Xml中,给对应控件添加样式:以TimePicker为例,添加  android:background="@drawable/my_style" /> 个:

    <TimePicker
         android:id="@+id/timePicker1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/button1"
         android:layout_below="@+id/button1"
         android:layout_marginTop="41dp"
         android:background="@drawable/my_style" />
 

当然了,给按钮也呆以添加 android:background="@drawable/my_style" 来应用样式。

3、效果如下:

。。。好了,发挥你的才智吧。我这里只是抛砖引玉,思路就是这样,大家好好发挥。

本文来自宋兴柱博客员:http://www.cnblogs.com/songxingzhu/

原文地址:https://www.cnblogs.com/songxingzhu/p/2713594.html