2

最近、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ウィザードによって作成された単純なスクラッチプロジェクトです。

4

2 に答える 2

4

追加したカスタム属性は R.java では使用できません カスタム属性を作成する主なモットーは、複数の場所で使用することだと思います。しかし、このコードでは、同じシナリオを達成することはできません。

これがサンプルコードです - attrs.xml ファイル

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="MyLayout">
    <attr name="text" format="string" />
 </declare-styleable>
</resources>

text 属性を追加するために main.xml を変更しています

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:red="http://red.com/ui/scheme"
  xmlns:myapp="http://schemas.android.com/apk/res/com.psl"
  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"
    myapp:text="Text String" />

<com.psl.TestView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffAABBCC"
    myapp:text="My Special Text String"
    red:cutom="customvalue" />

</LinearLayout>

TestView.java -

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"));
}

TypedArray a = context.obtainStyledAttributes(attrs,
        R.styleable.MyLayout);
CharSequence s = a.getString(R.styleable.MyLayout_text);
Log.d("MyTestView", "attrs param value: " + s.toString());
}
}

attrs.xml で attr を作成した後に気づいた場合。どこでも利用できます。ただし、カスタム名前空間を介して xml 自体で定義された attr は、どこでも定義する必要がある名前空間を介してのみ使用できます。属性がアプリケーション自体ではなくカスタム名前空間に追加されるため、バグである可能性があります。

于 2012-07-19T05:44:24.897 に答える
0

もちろん、これは「バグ」ではありません。これは、xmlでカスタムビューを使用する方法です。これを参照してください:http://developer.android.com/guide/topics/ui/custom-components.html

于 2012-07-19T05:48:42.390 に答える