カスタムコンポーネントを作成して、TextViewsをほとんど入れないようにしました。コンポーネントのJavaコードからカスタムディメンション属性を読み取る方法
<resources>
<declare-styleable name="BasicGauge">
<attr name="valueTextSize" format="dimension" />
<attr name="titleTextSize" format="dimension" />
<attr name="unitsTextSize" format="dimension" />
</declare-styleable>
</resources>
サンプルの初期化:
<pl.com.digita.BikeComputerUi.customviews.BasicGauge
android:id="@+id/basicGauge1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
valueTextSize="40sp">
</pl.com.digita.BikeComputerUi.customviews.BasicGauge>
今、私はテレビの
マイ属性定義のごとに独立してテキストサイズを渡し、コードから直接私のカスタムコントロールを初期化することができるようにしたいです
コンポーネントのコンストラクタでこれらの属性を読み取る方法:
final int N = typedArray.getIndexCount();
for (int i = 0; i < N; i++) {
int attribute = typedArray.getIndex(i);
switch (attribute) {
case R.styleable.BasicGauge_valueTextSize:
valueTextSize = typedArray.getString(attribute);
break;
case R.styleable.BasicGauge_titleTextSize:
titleTextSize = typedArray.getString(attribute);
break;
case R.styleable.BasicGauge_unitsTextSize:
unitsTextSize = typedArray.getString(attribute);
break;
}
typedArray.recycle();
}
問題: 作成後、私のすべての値はまだnullです。 40spは私の希望する値です。
まずは、私の問題を解決していただきありがとうございます。複合コンポーネントの内部でTextViewに寸法値を渡す最善の方法は何ですか? – piotrpo
あなたのやり方:カスタム属性を使用して、複合ビューの作成中にカスタム属性を取得し、作成または拡張時にビューに渡します。 – Snicolas