2015-07-02 30 views
1

更新:有CustomTextView定義。2個自定義屬性如果兩者都在XML它工作得很好定義。如果第一缺少它沒有給出第二個也是任何價值...如何獲取在xml佈局中定義的顏色屬性的引用?

<com.mycompany.projectname.MyCustomView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:extraColor="?someColor" 
/> 

這裏someColor是另一種顏色教師責任觀的不同的主題變化.. 我需要MyCustomView類extraColor自定義屬性的值...

目前獲得它如下:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, defStyleAttr, 0); 

a.getColorStat ELIST()不在這裏工作了......

CustomTextView定義爲:

<declare-styleable name="CustomTextView"> 
     <attr name="state" format="boolean" /> 
     <attr name="extraColor" format="reference|color" /> 
</declare-styleable> 

更新:有CustomTextView定義2個自定義屬性.. 如果兩者都在XML定義它工作得很好。如果首先是缺少它沒有給出第二個也是任何價值...

+1

你嘗試 「一getColorStateList(R.styleable.CustomTextView_extraColor)。」? –

+0

a。 getColorStateList(R.styleable.CustomTextView_extraColor);工作良好..但只有當這兩個自定義屬性定義在xml .. – Awesome

回答

1

你能嘗試的..

a.getColor(R.styleable.CustomTextView_extraColor, Color.WHITE) 
+1

不起作用...總是返回0 ... – Awesome

0

只使用兩種參考顏色

<declare-styleable name="CustomTextView"> 
     <attr name="extraColor" format="reference" /> 
</declare-styleable> 

然後,如果它是參考讓它像,

a. getColorStateList(R.styleable.CustomTextView_extraColor); 

如果是顏色的話,

a. getColor(R.styleable.CustomTextView_extraColor, Color.WHITE); 
0

除了別人怎麼說,它看起來像命名是不一致的(複製+粘貼錯誤?):在你發佈的大部分代碼中,你指的是CustomTextView,但是我在XML中,您使用MyCustomView

試着改變你的XML:

<com.mycompany.projectname.CustomTextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:extraColor="?someColor" 
/> 
+0

如果第一個屬性被定義爲像app:state =「true」..它的作品完美。 。 – Awesome