カスタム属性の値を親ビューから子ビューに「カスケード」するにはどうすればよいですか?
これは、例を使用して説明するのが最も簡単です。
<com.example.CustomLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:percent="35" >
<com.example.CustomView
android:id="@+id/customView1"
app:percent="how-to-get-app:percent-value-here???"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.example.CustomLayout>
ここで、CustomLayout
extends LinearLayout
. 要素のattrs.xml
使用でカスタム属性「パーセント」を定義しました。<declare-styleable>
ご覧のとおり、 の XML でパーセントを 35 に設定していますCustomLayout
。
私は今、同じ値をCustomView
(拡張するView
) に渡し、これを に含めたいと考えていCustomLayout
ます。XML でこれを行う方法を見つけることができません (ただし、コードでこれを行うのは簡単です)。
私は次のことを試しました:
app:percent="@attr/percent"
app:percent="?attr/percent"
これらはどちらも (予想どおり) NumberFormatException
at で失敗しTypedArray#getInt()
ます。
それで、これを機能させる方法についてのアイデアはありますか?