2013-07-17 48 views
17

我有一個自定義視圖,其中我想設置textview的顏色。自定義屬性獲取顏色返回無效值

attrs.xml

<declare-styleable name="PropertyView"> 
    <attr name="propertyTitle" format="string" localization="suggested" /> 
    <attr name="showTitle" format="boolean" /> 
    <attr name="propertyTextColor" format="color" /> 
    <attr name="propertyTextSize" format="dimension" /> 
</declare-styleable> 

我將它設置在佈局文件

<com.something.views.PropertyView 
    android:id="@+id/dwf_rAwayTeamTimePenaltyInput" 
    style="@style/mw" 
    propertyview:propertyTextSize="16sp" 
    propertyview:propertyTitle="@string/AwayTeam" 
    propertyview:showTitle="true" 
    propertyview:propertyTextColor="@color/textLight" /> 

而且在我的代碼設置

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PropertyView, 0, 0); 

    showTitle = a.getBoolean(R.styleable.PropertyView_showTitle, false); 
    String title = a.getString(R.styleable.PropertyView_propertyTitle); 
    float textSize = a.getDimension(R.styleable.PropertyView_propertyTextSize, -1); 
    int color = a.getColor(R.styleable.PropertyView_propertyTextColor, -1); 
    textSize = textSize/getResources().getDisplayMetrics().scaledDensity; 
    if(BuildConfig.DEBUG) Log.e(getClass().getName(), "Color set to: " + color); 

    setShowTitle(showTitle); 
    setTitle(title); 
    if(textSize >= 0) mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize); 
    if(color != -1) mTitleTextView.setTextColor(color); 

    a.recycle(); 

,但顏色保持返回-1。 我也試圖設置顏色爲#000 當我這樣做,我得到的-16777216

值我也試過這個問題或建議a.getInteger和a.getInt

任何經驗?

解決方案,感謝Alex富

的getColor不能處理引用

它與

ColorStateList color = a.getColorStateList(R.styleable.PropertyView_propertyTextColor); 
mTitleTextView.setTextColor(color); 

回答

16

現在的工作您使用的是在你的榜樣顏色的參考,但是根據到您的attrs.xml文件,該屬性必須是顏色類型,而不是引用。這可能是你使用十六進制顏色代碼的原因,但使用返回-1的引用。

如果您確實將格式更改爲引用,則還應更改方法以將其從a.getColor()更改爲a.getColorStateList()

+0

善於思考,但可惜它並沒有解決它。 –

+0

您是否將格式更改爲attrs.xml中的引用?如果是這樣,你是否也改變了'a.getColor()'方法?你應該嘗試使用'a.getColorStateList()'來代替。 'getColorStateList'可以理解純色和參考。 –

+0

謝謝你,這是=)愚蠢getColor無法處理引用,但它可能有一個很好的理由。 –

2

這是一些與attrs錯誤。

以下工作完美。


attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <!-- Your View --> 
    <declare-styleable name="YourView"> 
     <attr name="tint_color" format="reference" /> <!-- Important --> 
     <attr name="ripple_drawable" format="reference" /> <!-- Important --> 
    </declare-styleable> 

</resources> 

YourView.java

public YourView(Context context) { 
    this(context, null); 
} 

public YourView(Context context, @Nullable AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

public YourView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 

    // Get attrs 
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.YourView, defStyleAttr, 0); 

    // Set tint 
    int tintStyle = R.styleable.YourView_tint_color; 
    if (a.hasValue(tintStyle)) { 
     mTintColor = a.getResourceId(tintStyle, 0); // Important 
     setTint(mTintColor); 
    } 

    // Set Ripple 
    int rippleStyle = R.styleable.YourView_ripple_drawable; 
    if (a.hasValue(rippleStyle)) { 
     mRippleDrawable = a.getResourceId(rippleStyle, 0); // Important 
     setRipple(mRippleDrawable); 
    } 

    // End 
    a.recycle(); 
} 

使用

<com.your.app.YourView 
    ... 
    app:ripple_drawable="@drawable/ripple_default" 
    app:tint_color="@color/colorWhite" />