最近、xmlレイアウトのビューにカスタムxmlパラメーターを追加することに直面しました。この目的でattrs.xmlファイルを使用する必要があることはわかっていますが、attrs.xmlファイルがまったくなくてもカスタムパラメータを使用できることがわかりました。誰かがこれを説明できますか?これはバグですか、それとも有効なケースですか?
これが私のカスタムビューです:
public class TestView extends View {
public TestView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public TestView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
final String scheme = "http://red.com/ui/scheme";
if (attrs != null) {
Log.d("TestView", "custom param value: " + attrs.getAttributeValue(scheme, "cutom"));
}
}
}
これがメインのレイアウトです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:red="http://red.com/ui/scheme"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<com.red.ui.TestView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffAABBCC"
red:cutom="customvalue"
/>
</LinearLayout>
これは、Androidウィザードによって作成された単純なスクラッチプロジェクトです。