2013-07-17 8 views
17

Mam niestandardowy widok, w którym chcę ustawić kolor tekstu.Niestandardowe attr zwraca nieprawidłowe wartości kolorów

mam

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> 

ustawić go w pliku layoutu

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

I w moim kodzie ustawić go

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(); 

Ale kolor powraca zwracając -1. Próbowałem też ustawić kolor na # 000 Kiedy to zrobić i uzyskać wartość -16777216

Próbowałem też a.getInteger i a.getInt

, ktoś doświadczenie z tego problemu lub sugestie?

Rozwiązanie, dzięki Alex Fu

GetColor może nie obsługiwać referencje

Pracuje teraz z

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

Odpowiedz

16

Używasz odniesienie do koloru w swoim przykładzie, jednak według do pliku attrs.xml, ta właściwość musi być typu koloru, a nie odniesienia. Jest to prawdopodobnie powód, dla którego użyłeś kodu koloru szesnastkowego, który działał, ale używając referencji zwróconej -1.

Jeśli zmienisz format na odniesienie, powinieneś również zmienić metodę pobierania z a.getColor() na a.getColorStateList().

+0

Dobry pomysł, ale szkoda, że ​​nie rozwiązuje go. –

+0

Czy zmienił się format na odniesienie w pliku attrs.xml? Jeśli tak, czy zmieniłeś również metodę 'a.getColor()'? Zamiast tego należy spróbować użyć 'a.getColorStateList()'. 'getColorStateList' może zrozumieć zarówno kolory stałe, jak i odniesienia. –

+0

Dzięki, że to było =) głupie, że getColor nie może obsłużyć odwołania, ale prawdopodobnie ma dobry powód. –

2

Jest to rodzaj błędu z attrs.

Poniższe działa idealnie.


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(); 
} 

Wykorzystanie

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