Skip to content

Commit 27ed07b

Browse files
committed
Merge pull request tangqi92#16 from troyliu0105/master
replace unavailable link
2 parents 71cc82a + de0bcef commit 27ed07b

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
## 目录
1818

19-
* [A](#a)
19+
* [A](#a)
2020
* [B](#b)
2121
* [C](#c)
2222
* [D](#d)
@@ -531,7 +531,7 @@
531531
### 自定义控件
532532

533533
1. [Android 自定义View及其在布局文件中的使用示例](http://www.cnblogs.com/crashmaker/p/3521310.html)
534-
2. [自定义控件进阶:declare-styleable重用attr](http://droidyue.com/blog/2014/07/16/better-in-android-include-attrs-in-declare-stylable/)
534+
2. [自定义控件进阶:declare-styleable重用attr](https://github.com/troyliu0105/Android-Tips/blob/master/Utils/Content/declare-styleable_reuse_attr.md)
535535
3. [android 自定义控件 使用declare-styleable进行配置属性(源码角度)](http://blog.csdn.net/vipzjyno1/article/details/23696537)
536536
4. [Android使用AttributeSet自定义控件的方法](http://www.cnblogs.com/zwl12549/archive/2011/04/13/2015366.html)
537537
5. [从源码中浅析Android中如何利用attrs和styles定义控件](http://michaelye1988.iteye.com/blog/1773997)
@@ -561,7 +561,7 @@
561561

562562
## Contributors
563563

564-
- [troyliu0105](https://github.com/troyliu0105)
564+
- [troyliu0105](https://github.com/troyliu0105)
565565
- [whiskeyfei](https://github.com/whiskeyfei)
566566
- [wavever](https://github.com/wavever)
567567
- [nitta-honoka](https://github.com/nitta-honoka)
@@ -571,9 +571,8 @@
571571

572572
## Contact Me
573573

574-
Born in 1992, now a student of Southeast University, master of software engineerin. Loving technology, programming, reading and sports.
574+
Born in 1992, now a student of Southeast University, master of software engineerin. Loving technology, programming, reading and sports.
575575

576576
I will graduate in June 2017, expect the internship or full-time job in Android or iOS.
577577

578578
If you have any questions or want to make friends with me, please feel free to contact me : [imtangqi#gmail.com](mailto:[email protected] "Welcome to contact me")
579-
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[原文链接](http://droidyue.com/blog/2014/07/16/better-in-android-include-attrs-in-declare-stylable/)(*需要科学上网*)
2+
3+
## 自定义控件进阶:declare-styleable重用attr
4+
5+
最近接触了Android自定义控件,涉及到自定义xml中得属性(attribute),其实也很简单,但是写着写着,发现代码不完美了,就是在attrs.xml这个文件中,发现属性冗余,于是就想有没有类似属性继承或者include之类的方法.本文将就declare-stylable中属性重用记录一下.
6+
7+
---
8+
9+
### 不完美的代码
10+
11+
```xml
12+
<?xml version="1.0" encoding="utf-8"?>
13+
<resources>
14+
15+
<declare-styleable name="ExTextView">
16+
<attr name="enableOnPad" format="boolean" />
17+
<attr name="supportDeviceType" format="reference"/>
18+
</declare-styleable>
19+
20+
<declare-styleable name="ExEditText">
21+
<attr name="enableOnPad" format="boolean" />
22+
<attr name="supportDeviceType" format="reference"/>
23+
</declare-styleable>
24+
</resources>
25+
```
26+
27+
如上面代码,在ExTextView和ExEditText这个stylable中有着重复的属性申明.虽然上面可以工作,但是总感觉写的不专业,于是寻找优化方法.
28+
29+
---
30+
31+
### 还可以这样么
32+
33+
尝试着为declare-stylable指定一个parent,如下代码
34+
35+
```xml
36+
<?xml version="1.0" encoding="utf-8"?>
37+
<resources>
38+
39+
<declare-styleable name="ExTextView">
40+
<attr name="enableOnPad" format="boolean" />
41+
<attr name="supportDeviceType" format="reference"/>
42+
</declare-styleable>
43+
44+
<declare-styleable name="ExEditText" parent="ExTextView">
45+
46+
</declare-styleable>
47+
</resources>
48+
```
49+
50+
attrs.xml没有报告语法错误.但是当我使用R.styleable.ExEditText_supportDeviceType时候,R文件却没有生成,重新清理了工程还是不生效,不知道是否为adt插件的问题.其他人也遇到了这样的问题. 这个方法目前是不行的.
51+
52+
---
53+
54+
### 终极答案
55+
56+
实际上我们可以在declare-stylable之前,申明要多次使用的属性,在declare-stylable节点内部,只需调用即可.具体代码如下.
57+
58+
```xml
59+
<?xml version="1.0" encoding="utf-8"?>
60+
<resources>
61+
<attr name="enableOnPad" format="boolean" />
62+
<attr name="supportDeviceType" format="reference"/>
63+
64+
<declare-styleable name="ExTextView">
65+
<attr name="enableOnPad"/>
66+
<attr name="supportDeviceType"/>
67+
</declare-styleable>
68+
69+
<declare-styleable name="ExEditText">
70+
<attr name="enableOnPad"/>
71+
<attr name="supportDeviceType"/>
72+
</declare-styleable>
73+
</resources>
74+
```
75+
76+
每次引用attr后,建议清理一下工程,确保R文件重新生成.
77+
78+
#### 延伸阅读
79+
80+
+ [http://stackoverflow.com/questions/18827875/how-to-declare-several-stylable-attributes-with-the-same-name-for-different-tags](http://stackoverflow.com/questions/18827875/how-to-declare-several-stylable-attributes-with-the-same-name-for-different-tags)
81+
82+
#### 其他
83+
84+
+ [Android系统源代码情景分析](http://www.amazon.cn/gp/product/B009OLU8EE/ref=as_li_tf_tl?ie=UTF8&camp=536&creative=3200&creativeASIN=B009OLU8EE&linkCode=as2&tag=droidyue-23)

0 commit comments

Comments
 (0)