如何在选择/焦点上添加阴影到TextView

kle*_*mzy 17 android

我的问题是如何在选择TextView时向文本添加阴影或者选择View TextView.例如,我有一个CheckedTextView,它根据选择的类型改变背景.我还制作了一个文本选择器,可以改变不同状态的颜色.现在我想添加一个阴影,例如View被选中.因此它会改变背景颜色,文本颜色并创建阴影.这是我的文字选择器:

<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"/>
Run Code Online (Sandbox Code Playgroud)

和风格:

<style name="DarkShadow">   
    <item name="android:shadowColor">#BB000000</item>
    <item name="android:shadowRadius">2.75</item>
</style>
Run Code Online (Sandbox Code Playgroud)

现在文本被正确突出显示但没有阴影出现.有谁知道如何解决这个问题?

Gil*_*ert 21

这是Android SDK的当前限制.我扩展TextView了它的工作,你可以自由使用它:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

您还需要将其添加到attr.xml(或创建一个):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>
Run Code Online (Sandbox Code Playgroud)

所以最后你将能够在你的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"/>
Run Code Online (Sandbox Code Playgroud)

@color/btn_green_shadow_color点选择这样一个这样的:

<?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>
Run Code Online (Sandbox Code Playgroud)

如果您不熟悉如何使用自定义属性(使用我使用的自定义xml命名空间),请参阅这个好的StackOverFlow问题.


rau*_*aug 3

是的,我遇到了同样的问题,您可以使用 xml 中的选择器更改文本颜色,但不能更改阴影颜色。所以为了解决这个问题,你可能必须扩展CheckedTextView或者任何你需要的View,然后onDraw(Canvas canvas)根据View的状态重写因此,你需要使用 这里public void setShadowLayer (float radius, float dx, float dy, int color)定义的

例如:

@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);
}
Run Code Online (Sandbox Code Playgroud)

我希望这有效