質問編集履歴
2
RelativeLayoutから移植したときの間違いを修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -38,12 +38,6 @@
|
|
38
38
|
|
39
39
|
android:layout_height="wrap_content"
|
40
40
|
|
41
|
-
android:layout_alignParentLeft="true"
|
42
|
-
|
43
|
-
android:layout_alignParentStart="true"
|
44
|
-
|
45
|
-
android:layout_alignParentTop="true"
|
46
|
-
|
47
41
|
app:mySrc="@drawable/example"
|
48
42
|
|
49
43
|
app:myText="Example"/>
|
1
使い方の訂正\(リソース指定を明記\)
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,166 +18,180 @@
|
|
18
18
|
|
19
19
|
```xml
|
20
20
|
|
21
|
+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
22
|
+
|
23
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
24
|
+
|
25
|
+
android:layout_width="match_parent"
|
26
|
+
|
27
|
+
android:layout_height="match_parent"
|
28
|
+
|
29
|
+
android:orientation="vertical">
|
30
|
+
|
31
|
+
|
32
|
+
|
21
|
-
<include layout="@layout/my_image_text_view"
|
33
|
+
<include layout="@layout/my_image_text_view"
|
22
|
-
|
34
|
+
|
23
|
-
android:id="@+id/cafeImageView"
|
35
|
+
android:id="@+id/cafeImageView"
|
36
|
+
|
37
|
+
android:layout_width="wrap_content"
|
38
|
+
|
39
|
+
android:layout_height="wrap_content"
|
40
|
+
|
41
|
+
android:layout_alignParentLeft="true"
|
42
|
+
|
43
|
+
android:layout_alignParentStart="true"
|
44
|
+
|
45
|
+
android:layout_alignParentTop="true"
|
46
|
+
|
47
|
+
app:mySrc="@drawable/example"
|
48
|
+
|
49
|
+
app:myText="Example"/>
|
50
|
+
|
51
|
+
</LinearLayout>
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
---
|
58
|
+
|
59
|
+
java/com.example.mine/view/MyImageTextView.java
|
60
|
+
|
61
|
+
```Java
|
62
|
+
|
63
|
+
package com.example.mine.view;
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
/* import 文は省略*/
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
public class MyImageTextView extends FrameLayout {
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
private AttributeSet attrs;
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
public MyImageTextView(Context context, AttributeSet attrs) {
|
80
|
+
|
81
|
+
super(context, attrs);
|
82
|
+
|
83
|
+
this.attrs = attrs;
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
@Override
|
90
|
+
|
91
|
+
protected void onFinishInflate() {
|
92
|
+
|
93
|
+
super.onFinishInflate();
|
94
|
+
|
95
|
+
init();
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
private void init() {
|
102
|
+
|
103
|
+
final TextView textView = (TextView) findViewById(R.id.myTextView);
|
104
|
+
|
105
|
+
final ImageView imageView = (ImageView) findViewById(R.id.myImageView);
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
final TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyImageTextView);
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
final Drawable resourceId = typedArray.getDrawable(R.styleable.MyImageTextView_mySrc);
|
114
|
+
|
115
|
+
if(resourceId!=null)
|
116
|
+
|
117
|
+
imageView.setImageDrawable(resourceId);
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
final String text = typedArray.getString(R.styleable.MyImageTextView_myText);
|
122
|
+
|
123
|
+
textView.setText(text);
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
typedArray.recycle();
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
```
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
res/layout/my_image_text_view.xml
|
140
|
+
|
141
|
+
```xml
|
142
|
+
|
143
|
+
<com.example.mine.view.MyImageTextView xmlns:android="http://schemas.android.com/apk/res/android"
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
24
148
|
|
25
149
|
android:layout_width="wrap_content"
|
26
150
|
|
27
|
-
android:layout_height="wrap_content"
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
android
|
32
|
-
|
33
|
-
a
|
34
|
-
|
35
|
-
a
|
36
|
-
|
37
|
-
a
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
this.attrs = attrs;
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
@Override
|
76
|
-
|
77
|
-
protected void onFinishInflate() {
|
78
|
-
|
79
|
-
super.onFinishInflate();
|
80
|
-
|
81
|
-
init();
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
private void init() {
|
88
|
-
|
89
|
-
final TextView textView = (TextView) findViewById(R.id.myTextView);
|
90
|
-
|
91
|
-
final ImageView imageView = (ImageView) findViewById(R.id.myImageView);
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
final TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyImageTextView);
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
final Drawable resourceId = typedArray.getDrawable(R.styleable.MyImageTextView_mySrc);
|
100
|
-
|
101
|
-
if(resourceId!=null)
|
102
|
-
|
103
|
-
imageView.setImageDrawable(resourceId);
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
final String text = typedArray.getString(R.styleable.MyImageTextView_myText);
|
108
|
-
|
109
|
-
textView.setText(text);
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
typedArray.recycle();
|
114
|
-
|
115
|
-
}
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
```
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
res/layout/my_image_text_view.xml
|
151
|
+
android:layout_height="wrap_content">
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
<!--xmlns:app="http://schemas.android.com/apk/res-auto"-->
|
156
|
+
|
157
|
+
<ImageView
|
158
|
+
|
159
|
+
android:id="@+id/myImageView"
|
160
|
+
|
161
|
+
android:layout_width="match_parent"
|
162
|
+
|
163
|
+
android:layout_height="match_parent"
|
164
|
+
|
165
|
+
android:layout_gravity="center"
|
166
|
+
|
167
|
+
android:scaleType="fitXY" />
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
<TextView
|
172
|
+
|
173
|
+
android:id="@+id/myTextView"
|
174
|
+
|
175
|
+
android:layout_width="wrap_content"
|
176
|
+
|
177
|
+
android:layout_height="wrap_content"
|
178
|
+
|
179
|
+
android:layout_gravity="center"
|
180
|
+
|
181
|
+
android:textAppearance="?android:attr/textAppearanceLarge" />
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
</com.example.mine.view.MyImageTextView>
|
186
|
+
|
187
|
+
```
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
res/values/attrs.xml
|
126
192
|
|
127
193
|
```xml
|
128
194
|
|
129
|
-
<com.example.mine.view.MyImageTextView xmlns:android="http://schemas.android.com/apk/res/android"
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
xmlns:app="http://schemas.android.com/apk/res-auto"
|
134
|
-
|
135
|
-
android:layout_width="wrap_content"
|
136
|
-
|
137
|
-
android:layout_height="wrap_content">
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
<!--xmlns:app="http://schemas.android.com/apk/res-auto"-->
|
142
|
-
|
143
|
-
<ImageView
|
144
|
-
|
145
|
-
android:id="@+id/myImageView"
|
146
|
-
|
147
|
-
android:layout_width="match_parent"
|
148
|
-
|
149
|
-
android:layout_height="match_parent"
|
150
|
-
|
151
|
-
android:layout_gravity="center"
|
152
|
-
|
153
|
-
android:scaleType="fitXY" />
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
<TextView
|
158
|
-
|
159
|
-
android:id="@+id/myTextView"
|
160
|
-
|
161
|
-
android:layout_width="wrap_content"
|
162
|
-
|
163
|
-
android:layout_height="wrap_content"
|
164
|
-
|
165
|
-
android:layout_gravity="center"
|
166
|
-
|
167
|
-
android:textAppearance="?android:attr/textAppearanceLarge" />
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
</com.example.mine.view.MyImageTextView>
|
172
|
-
|
173
|
-
```
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
res/values/attrs.xml
|
178
|
-
|
179
|
-
```xml
|
180
|
-
|
181
195
|
<?xml version="1.0" encoding="utf-8"?>
|
182
196
|
|
183
197
|
<resources>
|