2011-06-30 7 views

答えて

1

Here他のパラメータには、ドキュメントと例があり、onFocusChangeListenerを使用してください。

+0

私は、テキストシャドーパラメータ、android:shadowColor android:shadowDx、android:shadowRadius、android:shadowDyを設定しようとしています。 StateListDrawableは、TextView Formating ParametersではなくDrawableのみを設定できます。 – miguel

+0

http://developer.android.com/guide/topics/resources/color-list-resource.htmlあなたのニーズに合っています。 – xevincent

+0

色の状態リストはshadowColorで機能するかもしれませんが、他のパラメータは何のために浮動小数点数をとりますか? – miguel

1

実際には、TextView(など)の色のセレクタを持つことができます。

あなたが使うのTextViewクラスから拡張し、追加することができ、この:

CTOR:

mDecorator = isInEditMode() ? null : new TextViewDecorator(this, context, attrs, defStyle); 

がdrawableStateChanged:

@Override 
protected void drawableStateChanged() { 
    super.drawableStateChanged(); 
    if (!isInEditMode()) 
     mDecorator.updateShadowColor(); 
} 

はまた新しいを追加するだけで、このソリューションを使用しますクラス "TextViewDecorator":

public class TextViewDecorator { 
    private final TextView mTextView; 
    private ColorStateList mShadowColors; 
    private float mShadowDx; 
    private float mShadowDy; 
    private float mShadowRadius; 

    public TextViewDecorator(final TextView textView, final Context context, final AttributeSet attrs, 
      final int defStyle) { 
     this.mTextView = textView; 
     initShadowsParams(context, attrs, defStyle); 
     updateShadowColor(); 
    } 

    private void initShadowsParams(final Context context, final AttributeSet attrs, final int defStyle) { 
     if (attrs != null) { 
      final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextView, defStyle, 0); 
      mShadowColors = a.getColorStateList(R.styleable.TextView_shadowColor); 
      mShadowDx = a.getDimension(R.styleable.TextView_shadowDx, 0); 
      mShadowDy = a.getDimension(R.styleable.TextView_shadowDy, 0); 
      mShadowRadius = a.getDimension(R.styleable.TextView_shadowRadius, 0); 
      a.recycle(); 
     } 
    } 

    public void updateShadowColor() { 
     if (mShadowColors == null) 
      return; 
     mTextView.setShadowLayer(mShadowRadius, mShadowDx, mShadowDy, 
       mShadowColors.getColorForState(mTextView.getDrawableState(), 0)); 
     mTextView.invalidate(); 
    } 
} 

およびattR:あなたはもちろん、あなたが使用しているものに「のTextView」の名前を変更する必要があり

<declare-styleable name="TextView"> 
    <attr name="shadowColor" format="color|reference" /> 
    <attr name="shadowDx" format="dimension" /> 
    <attr name="shadowDy" format="dimension" /> 
    <attr name="shadowRadius" format="dimension" /> 
</declare-styleable> 

注意。

関連する問題