原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://liangruijun.blog.51cto.com/3061169/732310

shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector。可以这样说,shape和selector在美化控件中的作用是至关重要的。

1.Shape

简介

作用:XML中定义的几何形状

位置:res/drawable/文件的名称.xml

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:android:background="@drawable/文件的名称"

属性:

<shape>  android:shape=["rectangle" | "oval" | "line" | "ring"]

其中rectagle矩形,oval椭圆,line水平直线,ring环形

<shape>中子节点的常用属性:

<gradient>  渐变

android:startColor  起始颜色

android:endColor  结束颜色

android:angle  渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0;

android:type  渐变的样式 liner线性渐变 radial环形渐变 sweep

<solid >  填充

android:color  填充的颜色

<stroke > 描边

android:width 描边的宽度

android:color 描边的颜色

android:dashWidth 表示'-'横线的宽度

android:dashGap 表示'-'横线之间的距离

<corners > 圆角

android:radius  圆角的半径 值越大角越圆

android:topRightRadius  右上圆角半径
  
android:bottomLeftRadius 右下圆角角半径
 
android:topLeftRadius 左上圆角半径

android:bottomRightRadius 左下圆角半径

 2.Selector

简介

位置:res/drawable/文件的名称.xml

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:android:background="@drawable/文件的名称"

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 默认时的背景图片-->
  4. <item android:drawable="@drawable/pic1" />
  5. <!-- 没有焦点时的背景图片 -->
  6. <item
  7. android:state_window_focused="false"
  8. android:drawable="@drawable/pic_blue"
  9. />
  10. <!-- 非触摸模式下获得焦点并单击时的背景图片 -->
  11. <item
  12. android:state_focused="true"
  13. android:state_pressed="true"
  14. android:drawable= "@drawable/pic_red"
  15. />
  16. <!-- 触摸模式下单击时的背景图片-->
  17. <item
  18. android:state_focused="false"
  19. android:state_pressed="true"
  20. android:drawable="@drawable/pic_pink"
  21. />
  22. <!--选中时的图片背景-->
  23. <item
  24. android:state_selected="true"
  25. android:drawable="@drawable/pic_orange"
  26. />
  27. <!--获得焦点时的图片背景-->
  28. <item
  29. android:state_focused="true"
  30. android:drawable="@drawable/pic_green"
  31. />
  32. </selector>

第一个例子:圆角的Button

http://liangruijun.blog.51cto.com/3061169/630051

第二个例子:shape+selector综合使用的例子 漂亮的ListView

先看看这个例子的结构:

selector.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_selected="true">
  4. <shape>
  5. <gradient android:angle="270" android:endColor="#99BD4C"
  6. android:startColor="#A5D245" />
  7. <size android:height="60dp" android:width="320dp" />
  8. <corners android:radius="8dp" />
  9. </shape>
  10. </item>
  11. <item android:state_pressed="true">
  12. <shape>
  13. <gradient android:angle="270" android:endColor="#99BD4C"
  14. android:startColor="#A5D245"/>
  15. <size android:height="60dp" android:width="320dp" />
  16. <corners android:radius="8dp" />
  17. </shape>
  18. </item>
  19. <item>
  20. <shape>
  21. <gradient android:angle="270" android:endColor="#A8C3B0"
  22. android:startColor="#C6CFCE"  />
  23. <size android:height="60dp" android:width="320dp" />
  24. <corners android:radius="8dp" />
  25. </shape>
  26. </item>
  27. </selector>

list_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:background="@drawable/selector"
  7. >
  8. <ImageView
  9. android:id="@+id/img"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_gravity="center_vertical"
  13. android:layout_marginLeft="20dp"
  14. />
  15. <TextView
  16. android:text="data"
  17. android:id="@+id/title"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:gravity="center_vertical"
  21. android:layout_marginLeft="20dp"
  22. android:layout_marginTop="20dp"
  23. android:textSize="14sp"
  24. android:textStyle="bold"
  25. android:textColor="@color/black"
  26. >
  27. </TextView>
  28. </LinearLayout>

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:background="#253853"
  7. >
  8. <ListView
  9. android:id="@+id/list"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:cacheColorHint="#00000000"
  13. android:divider="#2A4562"
  14. android:dividerHeight="3px"
  15. android:listSelector="#264365"
  16. android:drawSelectorOnTop="false"
  17. >
  18. </ListView>
  19. </LinearLayout>

colors.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="white">#FFFFFFFF</color>
  4. <color name="transparency">#00000000</color>
  5. <color name="title_bg">#1C86EE</color>
  6. <color name="end_color">#A0cfef83</color>
  7. <color name="black">#464646</color>
  8. </resources>

MainActivity.xml

  1. package com.lingdududu.customlist;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import xb.customlist.R;
  5. import android.R.array;
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.ListView;
  10. import android.widget.SimpleAdapter;
  11. public class MainActivity extends Activity {
  12. ListView list;
  13. String data[] = new String[]{
  14. "China","UK","USA","Japan","German","Canada","ET","Narotu"
  15. };
  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. list =(ListView) findViewById(R.id.list);
  22. SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list_item,
  23. new String[]{"title","img"}, new int[]{R.id.title,R.id.img});
  24. list.setAdapter(adapter);
  25. }
  26. private ArrayList<HashMap<String, Object>> getData() {
  27. ArrayList<HashMap<String, Object>> dlist = new ArrayList<HashMap<String,Object>>();
  28. for(int i =0;i<data.length;i++){
  29. HashMap<String, Object>map = new HashMap<String, Object>();
  30. map.put("title", data[i]);
  31. map.put("img", R.drawable.item_left2);
  32. dlist.add(map);
  33. }
  34. return dlist;
  35. }
  36. }

效果图:

上面的例子中用到很多关于颜色RGB的参数,对RGB不熟悉的,可以参照我博客的一篇文章

http://liangruijun.blog.51cto.com/3061169/629276

本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/732310

【Android进阶学习】shape和selector的结合使用(转)的更多相关文章

  1. 【Android进阶学习】shape和selector的结合使用

    shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...

  2. android 开发:shape和selector和layer-list的(详细说明)

    目录(?)[+] Shape 简介 使用的方法 属性 Selector 简介 使用的方法 layer-list 简介 例子 最后   <shape>和<selector>在An ...

  3. 【转】Android开发:shape和selector和layer-list的(详细说明)

    <shape>和<selector>在Android UI设计中经常用到.比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到<shape> ...

  4. [Android进阶]学习AccessibilityService实现微信抢红包插件

    在你的手机更多设置或者高级设置中,我们会发现有个无障碍的功能,很多人不知道这个功能具体是干嘛的,其实这个功能是为了增强用户界面以帮助残障人士,或者可能暂时无法与设备充分交互的人们 它的具体实现是通过A ...

  5. Android 进阶学习:事件分发机制全然解析,带你从源代码的角度彻底理解(上)

    http://blog.csdn.net/guolin_blog/article/details/9097463 事实上我一直准备写一篇关于Android事件分发机制的文章,从我的第一篇博客開始,就零 ...

  6. Android进阶——学习AccessibilityService实现微信抢红包插件

    在你的手机更多设置或者高级设置中,我们会发现有个无障碍的功能,很多人不知道这个功能具体是干嘛的,其实这个功能是为了增强用户界面以帮助残障人士,或者可能暂时无法与设备充分交互的人们 它的具体实现是通过A ...

  7. Android开发学习—— shape标签的使用

    参考这片文章http://www.cnblogs.com/armyfai/p/5912414.html

  8. Android开发:shape和selector和layer-list的(详细说明)

    http://blog.csdn.net/brokge/article/details/9713041

  9. Android开发教程:shape和selector的结合使用

    shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...

随机推荐

  1. linux中rz中的-e选项

    linux shell rz和sz是终端下常用的文件传输命令,rz和sz通过shell被调用,其中rz用于从启用终端的系统上传文件到目标系统(终端登录的目标系统), 这里不过多介绍这些命令,只是记录一 ...

  2. [Android]学习笔记之布局

    5大布局,其中前3个是常用的,第四个绝对布局已经提示deprecated ![](http://images2015.cnblogs.com/blog/194303/201611/194303-201 ...

  3. Win10 磁盘占用 100% 有效解决办法

    立即查看 任务管理器,看看是不是有一个 服务主机 unistack服务组或者找类似名称的,点开以后你会看到里面有同步主机 blablah请你毫不犹豫的结束它!结束它!结束它! 按下WIN+R调出运行, ...

  4. 分享公司DAO层动态SQL的一些封装

    主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...

  5. 利用AOP与ToStringBuilder简化日志记录

    刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...

  6. net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法

    使用Hibernate manytoone属性关联主表的时候,如果使用JSONArray把pojo对象转换成json对象时,很容易出现循环的异常.解决的办法就是, 在转换json对象时忽略manyto ...

  7. hdu1282回文数猜想

    Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数.任取一个正整数,如果不是回文数,将该数与他的倒序数相加,若其 ...

  8. Google地图路线规划

    Google地图路线规划: 需求:给定的两点之间Google地图路径规划和详情. 代码实现: //map定义省略 var directionsDisplay = new google.maps.Dir ...

  9. 写一个适应所有环境的js模块

    说下背景: 在ES6以前,JS语言没有模块化,如何让JS不止运行在浏览器,且能更有效的管理代码, 于是应运而生CommonJS这种规范,定义了三个全局变量: require,exports,modul ...

  10. linux 命令 之判断表达式

    摘自http://www.comptechdoc.org/os/linux/usersguide/linux_ugshellpro.html Tests There is a function pro ...