我想在text_focused(用Dpad突出显示)时为TextView添加阴影。
无法弄清楚如何使用XML执行此操作。没有“样式状态列表资源”
答案 0 :(得分:1)
Here您找到了doc和一个示例,并使用onFocusChangeListener作为其他参数。
答案 1 :(得分: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:
<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>
请注意,您当然应该更改&#34; TextView&#34;的名称。你用的是什么。