2012-06-10 173 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)获取文本。