View
xml レイアウトで宣言され、ビューのコンストラクター中に読み込まれるカスタムのスタイル可能な属性を作成したカスタム がいくつかあります。私の質問は、xml でレイアウトを定義するときにすべてのカスタム属性に明示的な値を指定しない場合、スタイルとテーマを使用して、View
コンストラクターに渡されるデフォルト値を設定するにはどうすればよいですか?
例えば:
attrs.xml:
<declare-styleable name="MyCustomView">
<attr name="customAttribute" format="float" />
</declare-styleable>
layout.xml (android:
簡単にするためにタグを削除):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.mypackage" >
<-- Custom attribute defined, get 0.2 passed to constructor -->
<com.mypackage.MyCustomView
app:customAttribute="0.2" />
<-- Custom attribute not defined, get a default (say 0.4) passed to constructor -->
<com.mypackage.MyCustomView />
</LinearLayout>