Resources.obtainAttributesで、CustomViewの独自属性の値を取得する。
Resources | Android Developers
activity_main.xmlは、
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <com.bgstation0.android.sample.resources_.CustomView android:id="@+id/customview1" android:layout_width="wrap_content" android:layout_height="wrap_content" app:custom_data="ABCDE" app:custom_number="1" /> </LinearLayout>
app:custom_dataを"ABCDE"、app:custom_numberを1とする。
attrs.xmlを追加し、
こう定義する。
あとは、CustomView.javaで、
res.obtainAttributesでaを取得し、a.getStringでmCustomData、a.getIntでmCustomNumberを取得、あとはmCustomDataを描画テキストの文字列、mCustomNumberを文字の大きさを決定する要素にする。
この状態では、
ABCDEで1なのでこうなる。
VWXYZで2だと、
こうなる。
テキストも変わってサイズも変わる。
Sample/android/Resources/obtainAttributes/src/Resources at master · bg1bgst333/Sample · GitHub