1

カスタム ビューと対応するカスタム属性を作成しました。例えば

<declare-styleable name="stripbar">
    <attr name="flowDirection" format="string"/>
</declare-styleable>

public Stripbar(Context context, AttributeSet attrs)
{
super(context, attrs);

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.stripbar);
CharSequence flowDirection = ta.getString(R.styleable.stripbar_flowDirection);

String text = attrs.getAttributeValue("android", "text"); 
}

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ns="http://schemas.android.com/apk/res/com.ns">

            <com.ns.Stripbar
                    android:id="@+id/aaa"
                    ns:flowDirection="RightToLeft"
                    android:text=”yoo hoo”/>

属性テキストに null 値が表示されます。私もこれを認識しており、問題を解決する方法がわかりません。

明確にするために、カスタム属性値とAndroidの定義済み属性値を並べて取得する必要がありますか?

何か助けはありますか?

4

1 に答える 1

3

問題は、 yourが実際には属性R.styleable.stripbarだけを含む整数の配列であることです。flowDirection

context.obtainStyledAttributes(attrs, R.styleable.stripbar);それを修正するには、を 置き換えることができるはずですcontext.obtainStyledAttributes(attrs, new int[]{R.attr.flowDirection, android.R.attr.text});

ta.getString( R.styleable.stripbar_flowDirection )次に、で置き換えてta.getString(0)テキストを取得できるはずですta.getString(1)

于 2012-09-11T22:56:57.177 に答える