1:shape总结

1):shape文件是放置在drawable文件下的。res/drawable/filename.xml.
2):shape类型:android:shape。

一共同拥有四种:rectangle。oval,line,ring。


3):corners标签:定义圆角。当且仅当控件类型位rectangle时才有作用。

android:radiuse位圆角的半径。

当然也能够单独为每一个圆角进行设置。


4):gradient标签:颜色渐变。
android:angle:颜色渐变的方向。0是从左到右;90度,是从下到上。

angle必须保证是45的倍数。


android:startColor,android:centerColor,android:endColor:分别为颜色的起始颜色。中间颜色和结束颜色。假设没有设置中间颜色,那么就从起始颜色渐变到结束颜色。否则回从起始颜色渐变到中间颜色,再从中间颜色渐变到结束颜色。
android:centerX。android:centerY:我认为这两个属性有存在的必要性的前提是要有 android:centerColor。原因是当我没有设置android:centerColor属性时,随意的改变这两个属性的值都不会改变控件的显示效果。这两个属性是渐变的中间位置。大小都是再0.0--1.0。

android:gradientRadius 渐变的半径大小。仅当渐变类型位radial的时候才有作用。


android:type 渐变的类型。有三种情况:linear,radial,sweep。
5):solid标签: 控件的颜色填充。
6):stroke标签:shape的线条。
android:width:线条的宽度。 android:color:线条的颜色。
android:dashWidth:线条的长度。当设置了该值时。将会显示位虚线。

除非你dashWidth设置的值非常大。那么看起来还是一条完整的线条。


android:dashGap:线条之间的距离。

仅仅在设置了dashWidth属性的情况下才会有效果。能够觉得是虚线之间的宽度。


7):padding标签:内边距。控件内容和控件四条边的距离。这里有点奇怪。等写完了控件样式定制再说吧。

2:控件样式定制


有时候android系统自带的控件类型看起来不是非常美观。有时我们须要自定义想要的样式效果。

此时就能够用到上面的shape drawable了。详细用法例如以下:

这里为一个button自己定义样式。

有一个默认button样式和一个button按下之后的样式。


首先定义两个shape文件:button_normal.xml和button_pressed.xml。
button_normal.xml:
<?xml version="1.0" encoding="utf-8"?

><shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"></corners> <solid android:color="#ff0000"/> <stroke android:width="1dp" android:color="#00ff00" android:dashWidth="2dp" android:dashGap="2dp"/></shape>


button_pressed.xml
<?

xml version="1.0" encoding="utf-8"?

><shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="10dp" ></corners> <!-- <solid android:color="#ffffff" ></solid> --> <padding android:left="20dp" android:top="1dp" ></padding> <gradient android:startColor="#ffffff" android:endColor="#000000" android:angle="0" android:centerX="0.1" android:centerY="0.2" /> <strokeandroid:width="1dp"android:color="#00ff00" android:dashWidth="1000dp"android:dashGap="3dp" > </stroke></shape>


眼下已经写好了两种样式文件了。

那么怎么推断是按下了的状态呢? 这里引入stateDrawable文件了。

它也是採用xml方式来定义的。在控件的不同状态能够用不同的样式来显示同一个控件。

比方:button有非常多种状态,按下状态,有焦点状态。和正常状态。


范例:button_style.xml
<?

xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/button_pressed"></item> <item android:drawable="@drawable/button_normal"></item> </selector>


这个文件会从上到下一直匹配下来。直到找到了一个item满足控件眼下的状态。android:state_pressed 是button被按下状态。

以下一个item是默认样式,能够匹配不论什么一种状态。所以要放在最以下。否则其它全部的定义都不起作用。


最后一步:给button加入样式。
 <Button        android:text="@string/button_style"         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/button_style"


这样就得到了button被按下和正常显示两种状态了。
还记得之前有说过padding非常奇怪吗?在button_normal.xml和button_pressed.xml两个文件里我仅仅定义了一个padding,可是无论在按钮处于哪种状态下。padding属性都会应用到button上。刚学习android。希望大神多多不吝赐教。


更多相关文章

  1. 【Android】如何让跑马灯跑起来-控件请求焦点
  2. Android控件GridView的使用
  3. 取消默认Listview点击的显示的颜色
  4. android 相对布局中的 控件布局
  5. Android中自定义switch控件样式
  6. android TextView的字体颜色设置的多种方法(续)
  7. android中listview控件覆盖了其它控件使下面的其它控件不显示
  8. 自定义控件 - 圆形缓冲进度条
  9. Android 的res/values/colors自定义颜色

随机推荐

  1. 说话时如何把“NO”变成“yes”?
  2. Azure DevTest Lab体验(二)用户测试
  3. 链路追踪 SkyWalking 源码分析 —— Coll
  4. 链路追踪 SkyWalking 源码分析 —— Coll
  5. 分布式作业系统 Elastic-Job-Cloud 源码
  6. 一文学会Vue中间件管道[每日前端夜话0x8C
  7. 链路追踪 SkyWalking 源码分析 —— Coll
  8. CPU 是怎样工作的?[每日前端夜话0x89]
  9. 造了一个 Redis 分布锁的轮子,没想到还学
  10. 链路追踪 SkyWalking 源码分析 —— Coll