在Android中的控件种类已经足够我们使用,但是有时候大家需要根据美工的设计来改变一些控件的颜色,式样,以及背景图片
最近正好有这方面的需要,用了很久时间,找到了改变基本颜色以及图片的方法
下面以SeekBar为例,为大家描述一下我的做法

首先在layout文件夹中的main.xml内容如下

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <SeekBarandroid:id="@+id/seek"android:layout_width="300px"
  6. android:layout_height="wrap_content"android:max="100"
  7. android:progress="50"android:progressDrawable="@drawable/seekbar_img"
  8. android:thumb="@drawable/thumb"/>
  9. </LinearLayout>


很简单,只有一个SeekBar控件,注意它的android:progressDrawable="@drawable/seekbar_img"以及android:thumb="@drawable/thumb"它们分别对应的是 进度条的图片以及拖动滑块的图片,这里的图片也可以是我们自定义的drawable中的xml文件,可以理解成这两个属性应该如何显示的意思,而@drawable/seekbar_img和@drawable/thumb它们分别对应着 drawable 文件夹中的文件seekbar_img.xml和thumb.xml,它们表示着如何去显示进度条与滑块

当初我想的是在网上找SeekBar的原始样式文件是如何定义,这样就可以照搬代码,修改一些我需要的图片以及颜色和大小就行了,于是就开始搜索,这里要用到的是Android的系统源码,具体下载办法网上很多,需要用到cygwin,大家可以参考http://tech.it168.com/a2009/0529/579/000000579026.shtml



下好源代以后,可以在 C:\cygwin\home\android\frameworks\base\core\res\res\drawable 这个路径下找到很多图片与android的原始控件样式(即xml文件)
找一下,哈哈,好东西可不少,以后要改样式全靠它们了
我们可以在这是里面找到seek_thumb.xml,内容如下

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <!--Copyright(C)2008TheAndroidOpenSourceProject
  3. LicensedundertheApacheLicense,Version2.0(the"License");
  4. youmaynotusethisfileexceptincompliancewiththeLicense.
  5. YoumayobtainacopyoftheLicenseat
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  8. distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  9. WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  10. SeetheLicenseforthespecificlanguagegoverningpermissionsand
  11. limitationsundertheLicense.
  12. -->
  13. <!--Thisisthethumbontheseekbar.-->
  14. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  15. <itemandroid:state_pressed="true"
  16. android:state_window_focused="true"
  17. android:drawable="@drawable/seek_thumb_pressed"/>
  18. <itemandroid:state_focused="true"
  19. android:state_window_focused="true"
  20. android:drawable="@drawable/seek_thumb_selected"/>
  21. <itemandroid:state_selected="true"
  22. android:state_window_focused="true"
  23. android:drawable="@drawable/seek_thumb_selected"/>
  24. <itemandroid:drawable="@drawable/seek_thumb_normal"/>
  25. </selector>

它定义的是seekbar的滑块样式,内容很简单,大家应该看得懂,分别对应着按下,选中,以及获得焦点时滑块的图片


另外,我们还可以找到 progress_horizontal.xml,内容如下

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <!--Copyright(C)2008TheAndroidOpenSourceProject
  3. LicensedundertheApacheLicense,Version2.0(the"License");
  4. youmaynotusethisfileexceptincompliancewiththeLicense.
  5. YoumayobtainacopyoftheLicenseat
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  8. distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  9. WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  10. SeetheLicenseforthespecificlanguagegoverningpermissionsand
  11. limitationsundertheLicense.
  12. -->
  13. <layer-listxmlns:android="http://schemas.android.com/apk/res/android">
  14. <itemandroid:id="@android:id/background">
  15. <shape>
  16. <cornersandroid:radius="5dip"/>
  17. <gradient
  18. android:startColor="#ff9d9e9d"
  19. android:centerColor="#ff5a5d5a"
  20. android:centerY="0.75"
  21. android:endColor="#ff747674"
  22. android:angle="270"
  23. />
  24. </shape>
  25. </item>
  26. <itemandroid:id="@android:id/secondaryProgress">
  27. <clip>
  28. <shape>
  29. <cornersandroid:radius="5dip"/>
  30. <gradient
  31. android:startColor="#80ffd300"
  32. android:centerColor="#80ffb600"
  33. android:centerY="0.75"
  34. android:endColor="#a0ffcb00"
  35. android:angle="270"
  36. />
  37. </shape>
  38. </clip>
  39. </item>
  40. <itemandroid:id="@android:id/progress">
  41. <clip>
  42. <shape>
  43. <cornersandroid:radius="5dip"/>
  44. <gradient
  45. android:startColor="#ffffd300"
  46. android:centerColor="#ffffb600"
  47. android:centerY="0.75"
  48. android:endColor="#ffffcb00"
  49. android:angle="270"
  50. />
  51. </shape>
  52. </clip>
  53. </item>
  54. </layer-list>



有了这两个文件的源代码,我们就可以依样画葫芦了
首先在自己的工程下drawable文件夹中建立seek_bar.xml文件与thumb.xml文件
我写的两个文件内容如下
seekbar_img.xml

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <layer-listxmlns:android="http://schemas.android.com/apk/res/android">
  3. <!--背景图-->
  4. <itemandroid:id="@+android:id/background"android:drawable="@drawable/bg"/>
  5. <!--全部能量图-->
  6. <itemandroid:id="@+android:id/SecondaryProgress"
  7. android:drawable="@drawable/bg"/>
  8. <!--进和能量图-->
  9. <itemandroid:id="@+android:id/progress"android:drawable="@drawable/bg2"/>
  10. </layer-list>

thumb.xml

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  3. <!--按下状态-->
  4. <itemandroid:state_pressed="true"android:drawable="@drawable/bg3"/>
  5. <!--普通无焦点状态-->
  6. <itemandroid:state_focused="false"android:state_pressed="false"
  7. android:drawable="@drawable/bg4"/>
  8. </selector>

装载自:http://www.iteye.com/topic/788978

更多相关文章

  1. Android 性能优化:使用 Lint 优化代码、去除多余资源
  2. 批量处理ios破解后的资源文件为android所用
  3. Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内
  4. 关于代码家(干货集中营)共享知识点汇总系列——Android
  5. android的EditText里文字和图片混合编辑与显示
  6. 《Android第一行代码》笔记
  7. Android中的R.java文件你知多少

随机推荐

  1. sql 多条件组合查询,并根据指定类别找出所
  2. sql 随机抽取几条数据的方法 推荐
  3. 对有insert触发器表取IDENTITY值时发现的
  4. SQL SERVER 查询正在实行的SQL语句
  5. SQL语句查询是否为空 =null及null
  6. SQLserver 实现分组统计查询(按月、小时分
  7. SQL Server日志过大会影响查询结果
  8. sqlserver 数据库日志备份和恢复步骤
  9. Android中Activity全局共享方法AppContex
  10. Android(安卓)基于agora 视频会议开发