2011-01-20 21 views
17

私の質問は、TextViewが選択されているか、TextViewが選択されているViewを選択したときに、影にテキストを追加する方法です。例えば、私は、選択の種類に応じて背景を変更するCheckedTextViewを持っています。私はまた、異なる状態で色を変えるテキストセレクタを作った。今度は、たとえばViewが選択されたときに影を追加したいと思います。そのため、背景色、テキストの色が変わり、影ができます。選択/フォーカスでTextViewに影を追加する方法

<selector 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<item 
    android:state_focused="true" 
    android:state_pressed="false"  
    android:color="@android:color/white" 
    style="@style/DarkShadow"/> 

<item 
    android:state_focused="true" 
    android:state_pressed="true"    
    android:color="@android:color/white" 
    style="@style/DarkShadow"/> 

<item 
    android:state_focused="false" 
    android:state_pressed="true" 
    android:color="@android:color/white" 
    style="@style/DarkShadow"/> 

<item 
    android:color="@color/primary_text_light_disable_only"/> 

とスタイル::これは私のテキストセレクタである

<style name="DarkShadow"> 
    <item name="android:shadowColor">#BB000000</item> 
    <item name="android:shadowRadius">2.75</item> 
</style> 

は今テキストが正しく強調表示されますが、影は表示されません。誰もこれを解決する方法を知っていますか?

+0

私は色状態一覧や国家リストDrawableのを知っていたが、影がTextViewに、特定のプロパティのようです。おそらくシャドー属性は無視されるでしょう。私はいくつかのonStateChangeメソッドを使用してビューを設定していましたが、影をdinamically設定しましたが、私はそれを探しました。私は次に尋ねる:ビューの状態の変更を取得することは可能ですか? – bigstones

答えて

3

ええ、同じ問題が発生しました。xmlのセレクタを使用してテキストカラーを変更できますが、シャドウカラーは変更できません。 だから、問題を解決するために、あなたはこのように、あなたが例えばhere

で定義されて public void setShadowLayer (float radius, float dx, float dy, int color)を使用する必要がCheckedTextViewを拡張したり、何が必要ビュー、[表示 の状態に応じてonDraw(Canvas canvas)オーバーライドする必要があります:

@Override 
protected void onDraw(Canvas canvas) { 
    if(isPressed()){ 
     setShadowLayer(1, 0, 1, Color.RED); 
    }else{ 
     if(isFocused()){ 
      setShadowLayer(1, 0, 1, Color.WHITE); 
     }else{ 
      setShadowLayer(1, 0, 1, Color.BLACK); 
     } 
    } 
    super.onDraw(canvas); 
} 

私はこれは、Android SDKの現在の制限です

+1

書かれているこのコードは動作しませんsetShadowLayerは再描画を終え、無限の描画ループに終わります。しかし、それをonFocusChangedに置くことも、インスタンス上にOnFocusChangeListenerを設定することもできます。 @ミゲルが合意した。 – miguel

+0

setShadowLayerはinvalidateを呼び出します。 – nmw

+0

どのようにそれを解決しましたか? – r4jiv007

21

の作品を願っています。

CustomTextView.java:

import android.widget.TextView; 
import android.util.AttributeSet; 
import android.content.res.TypedArray; 
import android.content.Context; 

import com.client.R; 


public class CustomTextView extends TextView 
{ 

    private static String TAG = "CustomTextView"; 

    private ColorStateList mShadowColors; 
    private float mShadowDx; 
    private float mShadowDy; 
    private float mShadowRadius; 


    public CustomTextView(Context context) 
    { 
     super(context); 
    } 


    public CustomTextView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     init(context, attrs); 
    } 


    public CustomTextView(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     init(context, attrs); 
    } 


    /** 
    * Initialization process 
    * 
    * @param context 
    * @param attrs 
    * @param defStyle 
    */ 
    private void init(Context context, AttributeSet attrs, int defStyle) 
    { 
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, defStyle, 0); 

     final int attributeCount = a.getIndexCount(); 
     for (int i = 0; i < attributeCount; i++) { 
      int curAttr = a.getIndex(i); 

      switch (curAttr) {     
       case R.styleable.CustomTextView_shadowColors: 
        mShadowColors = a.getColorStateList(curAttr); 
        break; 

       case R.styleable.CustomTextView_android_shadowDx: 
        mShadowDx = a.getFloat(curAttr, 0); 
        break; 

       case R.styleable.CustomTextView_android_shadowDy: 
        mShadowDy = a.getFloat(curAttr, 0); 
        break; 

       case R.styleable.CustomTextView_android_shadowRadius: 
        mShadowRadius = a.getFloat(curAttr, 0); 
        break; 

       default: 
       break; 
     } 
    } 

     a.recycle(); 

     updateShadowColor(); 
    } 

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

    @Override 
    protected void drawableStateChanged() 
    { 
     super.drawableStateChanged(); 
     updateShadowColor(); 
    } 
} 
あなたはまた、あなたのattr.xmlにこれを追加(または1つを作成)する必要があり

: のattrを は、私はそれを動作させるために、あなたは自由にそれを使用することができTextViewを拡張しました.xmlファイル:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="Theme"> 
     <attr format="reference" name="CustomTextView"/> 
    </declare-styleable> 

    <declare-styleable name="CustomTextView"> 
     <attr name="shadowColors" format="color|reference"/> 
     <attr name="android:shadowDx"/> 
     <attr name="android:shadowDy"/> 
     <attr name="android:shadowRadius"/> 

    </declare-styleable> 
</resources> 

だから、最終的に、あなたはこのように、あなたのXMLSでそれを使用することができます:

<com.client.ui.textviews.CustomTextView 
xmlns:client="http://schemas.android.com/apk/res/com.client" 
     android:id="@+id/join_text" 
     android:shadowDx="1" 
     android:shadowDy="1" 
     android:shadowRadius="1" 
     client:shadowColors="@color/btn_green_shadow_color"/> 
セレクタへ @color/btn_green_shadow_colorポイントは、このような、これは

:あなたは(私が使用したカスタムXML名前空間)カスタム属性の使用方法に精通していない場合

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_enabled="false" android:color="@android:color/white"/> 
    <item android:state_pressed="true" android:color="@color/BzDarkGray"/> 
    <item android:color="@android:color/black"/> 

</selector> 

this good StackOverFlow questionを参照してください。

+0

私はあなたの投稿を誤読したにちがいありません。恐らく別のものと混同してしまいました。私はshadowDx/Dy/Radiusの新しい属性を作成したと思いました。コメントを削除しました。ありがとう。 – nmw

+0

@Gilbertクラスcustomtextviewを拡張するバイナリXMLファイルのエラーが発生します。なぜどんなアイデア? –

+0

@AshleyStaggs私は、あなたがやろうとしていること、そしてあなたが得ているエラーについて少し詳しい情報が必要だと思います。 – Gilbert

2

これは私がやってしまったものです:

@Override 
protected void drawableStateChanged() { 
    super.drawableStateChanged(); 
    if(isPressed()) { 
     setShadowLayer(15, 0, 0, getTextColors().getDefaultColor()); 
    } else { 
     setShadowLayer(0, 0, 0, Color.TRANSPARENT); 
    } 
} 
関連する問題