2012-06-10 230 views
1

我創建了自定義視圖以及相應的自定義屬性。例如android在自定義視圖上並排定義屬性和自定義屬性

<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」/> 

我收到屬性文字空值。我也知道this,不知道如何解決這個問題。

爲了澄清,我需要獲得我的自定義屬性值以及android預定義的屬性值並排?

任何幫助?

回答

3

問題是,您的R.styleable.stripbar實際上是一個整數數組,其中只包含您的flowDirection屬性。

爲了解決這個問題,你應該能夠取代你context.obtainStyledAttributes(attrs, R.styleable.stripbar);context.obtainStyledAttributes(attrs, new int[]{R.attr.flowDirection, android.R.attr.text});

然後,你應該能夠ta.getString(0)取代ta.getString(R.styleable.stripbar_flowDirection)ta.getString(1)獲取文本。