2011-01-20 31 views
17

Câu hỏi của tôi là cách thêm bóng vào văn bản khi TextView được chọn hoặc Xem TextView đang được chọn. Ví dụ tôi có một CheckedTextView mà thay đổi nền theo loại lựa chọn. Tôi cũng đã tạo một bộ chọn văn bản thay đổi màu sắc trên các trạng thái khác nhau. Bây giờ tôi muốn thêm một cái bóng khi ví dụ View được chọn. Vì vậy, nó thay đổi màu nền, màu chữ và tạo bóng. Đây là chọn văn bản của tôi:Cách thêm bóng vào TextView khi chọn/lấy nét

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

và phong cách:

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

Bây giờ văn bản được nhấn mạnh đúng nhưng không có bóng xuất hiện. Có ai biết cách giải quyết vấn đề này không?

+0

tôi biết về Màu Danh sách Nhà nước và Danh sách nhà nước drawable, nhưng bóng tối dường như là một tài sản cụ thể của TextView. Có lẽ thuộc tính bóng chỉ bị bỏ qua. Tôi đã có thể sử dụng một số phương pháp onStateChange cho các khung nhìn, thiết lập bóng tối, nhưng tôi đã chỉ tìm nó và đáng ngạc nhiên nó chỉ tồn tại cho các drawables! Sau đó tôi sẽ hỏi: liệu có thể thay đổi trạng thái xem không? – bigstones

Trả lời

3

Vâng, tôi đã gặp phải vấn đề tương tự, bạn có thể thay đổi màu văn bản bằng cách sử dụng bộ chọn trong xml, nhưng không phải là shadowcolor. Vì vậy, để giải quyết vấn đề, bạn có thể phải kéo dài CheckedTextView hoặc bất cứ điều gì Xem bạn cần, và sau đó ghi đè onDraw(Canvas canvas) theo tình trạng của Xem Vì vậy, bạn cần phải sử dụng public void setShadowLayer (float radius, float dx, float dy, int color) quy định tại here

ví dụ :

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

tôi hy vọng rằng việc

+1

Mã này như được viết không có ý nghĩa làm việc setShadowLayer kết thúc vẽ lại để bạn kết thúc bằng một vòng lặp vô hạn. Nhưng bạn có thể đặt nó trong onFocusChanged hoặc thiết lập OnFocusChangeListener trên một thể hiện. – miguel

+0

@miguel đã đồng ý. setShadowLayer gọi vô hiệu. – nmw

+0

bạn đã giải quyết nó như thế nào? – r4jiv007

21

Đây là một hạn chế hiện tại của Android SDK. tôi mở rộng TextView cho nó hoạt động, bạn có thể sử dụng nó một cách tự do:

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

Bạn cũng cần để thêm video này attr.xml của bạn (hoặc tạo một): attr .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> 

Vì vậy, cuối cùng bạn sẽ có thể sử dụng nó trong XMLs của bạn, như thế này:

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

đâu @color/btn_green_shadow_color điểm đến một selector như một này:

<?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> 

Nếu bạn không quen thuộc với cách sử dụng thuộc tính tùy chỉnh (với không gian tên tùy chỉnh xml tôi đã sử dụng), vui lòng tham khảo this good StackOverFlow question.

+0

Tôi phải đọc nhầm bài đăng của bạn - có lẽ đã nhầm lẫn với bài đăng khác - tôi nghĩ bạn đã tạo các thuộc tính mới cho shadowDx/Dy/Radius. Tôi đã xóa nhận xét. Cảm ơn. – nmw

+0

@Gilbert Tôi nhận được lỗi tệp nhị phân XML đang tăng cấp độ tùy chỉnh lớp học. Bất kỳ ý tưởng tại sao? –

+0

@AshleyStaggs Tôi nghĩ rằng tôi cần thêm một chút thông tin về những gì bạn đang cố gắng làm và về lỗi bạn đang gặp phải. – Gilbert

2

Đây là những gì tôi đã kết thúc thực hiện:

@Override 
protected void drawableStateChanged() { 
    super.drawableStateChanged(); 
    if(isPressed()) { 
     setShadowLayer(15, 0, 0, getTextColors().getDefaultColor()); 
    } else { 
     setShadowLayer(0, 0, 0, Color.TRANSPARENT); 
    } 
} 
Các vấn đề liên quan