回答編集履歴
1
回答
answer
CHANGED
@@ -1,3 +1,61 @@
|
|
1
|
-
|
1
|
+
drawableリソースから利用するためのカスタム属性をattrリソースで定義してテーマで変更できるようにしたいなら、以下のような実装になると思います。
|
2
2
|
|
3
|
+
- drawable/shape.xml
|
4
|
+
|
5
|
+
```xml
|
6
|
+
<?xml version="1.0" encoding="utf-8"?>
|
7
|
+
<shape
|
8
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
9
|
+
android:shape="rectangle">
|
10
|
+
<solid
|
11
|
+
android:color="?solidColor"/>
|
12
|
+
<corners
|
13
|
+
android:topRightRadius="?radius"
|
14
|
+
android:bottomRightRadius="?radius"
|
15
|
+
android:bottomLeftRadius="?radius"
|
16
|
+
android:topLeftRadius="?radius"/>
|
17
|
+
<stroke
|
18
|
+
android:width="?strokeWidth"
|
19
|
+
android:color="?strokeColor"/>
|
20
|
+
</shape>
|
21
|
+
```
|
22
|
+
|
23
|
+
- values/attrs.xml
|
24
|
+
|
25
|
+
```xml
|
26
|
+
<?xml version="1.0" encoding="utf-8"?>
|
27
|
+
<resources>
|
28
|
+
<declare-styleable name="test">
|
29
|
+
<attr name="strokeColor" format="color" />
|
30
|
+
<attr name="strokeWidth" format="dimension" />
|
31
|
+
<attr name="solidColor" format="color" />
|
32
|
+
<attr name="radius" format="dimension" />
|
33
|
+
</declare-styleable>
|
34
|
+
</resources>
|
35
|
+
```
|
36
|
+
|
37
|
+
- values/styles.xml
|
38
|
+
|
39
|
+
```xml
|
40
|
+
<?xml version="1.0" encoding="utf-8"?>
|
41
|
+
<resources>
|
42
|
+
<style name="ButtonBackground1">
|
43
|
+
<item name="strokeColor">#ED586D</item>
|
44
|
+
<item name="strokeWidth">2dp</item>
|
45
|
+
<item name="solidColor">#ffffff</item>
|
46
|
+
<item name="radius">30dp</item>
|
47
|
+
</style>
|
48
|
+
</resources>
|
49
|
+
```
|
50
|
+
|
51
|
+
- レイアウト
|
52
|
+
|
53
|
+
```xml
|
54
|
+
<Button
|
55
|
+
...
|
56
|
+
android:background="@drawable/shape"
|
57
|
+
android:theme="@style/ButtonBackground1"
|
58
|
+
/>
|
59
|
+
```
|
60
|
+
|
3
|
-
|
61
|
+
ただし、API21未満だとdrawableリソースからcolorの参照ができなかったと思うので、その点だけご注意ください。
|