首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在选择/聚焦时将阴影添加到TextView

如何在选择/聚焦时将阴影添加到TextView
EN

Stack Overflow用户
提问于 2011-01-21 06:03:09
回答 3查看 14.5K关注 0票数 17

我的问题是,当TextView被选中或TextView所在的视图被选中时,如何在文本中添加阴影。例如,我有一个根据选择类型改变背景的CheckedTextView。我还做了一个文本选择器,可以改变不同状态下的颜色。现在,我想添加一个阴影,例如视图被选中时。所以它改变了背景颜色,文本颜色并创建了一个阴影。这是我的文本选择器:

代码语言:javascript
复制
<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"/>

和风格:

代码语言:javascript
复制
<style name="DarkShadow">   
    <item name="android:shadowColor">#BB000000</item>
    <item name="android:shadowRadius">2.75</item>
</style>

现在,文本可以正确高亮显示,但不会出现阴影。有人知道怎么解决这个问题吗?

EN

回答 3

Stack Overflow用户

发布于 2012-05-10 16:52:53

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

CustomTextView.java:

代码语言:javascript
复制
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 (或创建一个):attr.xml:

代码语言:javascript
复制
<?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中使用它,如下所示:

代码语言:javascript
复制
<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指向选择器,如@color/btn_green_shadow_color

代码语言:javascript
复制
<?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>

如果您不熟悉如何使用自定义属性(使用我使用的自定义xml命名空间),请参考this good StackOverFlow question

票数 21
EN

Stack Overflow用户

发布于 2011-02-09 05:34:25

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

例如:

代码语言:javascript
复制
@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);
}

我希望这能行得通

票数 3
EN

Stack Overflow用户

发布于 2014-02-05 21:55:11

这就是我最终要做的:

代码语言:javascript
复制
@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if(isPressed()) {
        setShadowLayer(15, 0, 0, getTextColors().getDefaultColor());
    } else {
        setShadowLayer(0, 0, 0, Color.TRANSPARENT);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4753158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档