【发布时间】:2014-02-02 18:57:53
【问题描述】:
我正在使用自定义视图来显示渐变颜色填充的 TextView,如下所示:
public class GradientTextView extends TextView {
public GradientTextView(Context context) {
super(context, null, -1);
}
public GradientTextView(Context context,
AttributeSet attrs) {
super(context, attrs, -1);
}
public GradientTextView(Context context,
AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode()) {
GradientTextView.getTextColor(context, null, defStyle);
}
}
@Override
protected void onLayout(boolean changed,
int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (changed) {
getPaint().setShader(new LinearGradient(
0, 0, 0, getHeight(),
Color.parseColor(Color.WHITE), Color.parseColor(Color.BLACK),
Shader.TileMode.CLAMP));
}
}
}
它在设备或模拟器上运行良好,但在 Android Studio 的 xml 预览器中出现渲染问题,建议在自定义视图中使用 View.isInEditMode() 来跳过代码或在 IDE 中显示示例数据并显示此错误堆栈:
java.lang.NullPointerException
谁能帮我解决这个问题?
【问题讨论】:
标签: xml layout android-custom-view