Java View.isInEditMode()不';t跳过代码
我已经创建了一个自定义视图。它在真实设备上工作得非常完美,但在设计模式下,eclipse说:Java View.isInEditMode()不';t跳过代码,java,android,eclipse,android-layout,android-custom-view,Java,Android,Eclipse,Android Layout,Android Custom View,我已经创建了一个自定义视图。它在真实设备上工作得非常完美,但在设计模式下,eclipse说: The following classes could not be instantiated: - com.test.MyBanner (Open Class, Show Error Log) See the Error Log (Window > Show View) for more details. Tip: Use View.isInEditMode() in your custom v
The following classes could not be instantiated:
- com.test.MyBanner (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
我的代码非常简单:
public class MyBanner extends WebView {
public MyBanner(Context context) {
super(context);
}
public MyBanner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyBanner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if(this.isInEditMode()){
getLayoutParams().height = 80;
}
else{
getLayoutParams().height = 80;
//Logotype----------------------------------------------
RelativeLayout rl = new RelativeLayout(getContext());
rl.setLayoutParams(getLayoutParams());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(100, 100);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ImageView logotype = new ImageView(getContext());
logotype.setBackgroundColor(Color.TRANSPARENT);
logotype.setImageResource(R.drawable.ic_launcher);
rl.addView(logotype, rlp);
ViewGroup v = (ViewGroup)MyBanner.this;
v.addView(rl); //If I comment this it works perfect.
}
}
}
xml:
如果我评论
v.addView(rl)代码>eclipse正确显示我的视图。尽管我使用了this.isInEditMode()
这个事实,但在我看来eclipse读取所有代码。也许,这是一个eclipse错误。如何使eclipse忽略超出此.isInEditMode()条件的代码行?我已替换了有问题的代码:
ViewGroup v = (ViewGroup)MyBanner.this;
v.addView(rl);
对以下一项:
this.addView(rl);
现在eclipse显示了我的视图,没有错误。Hear是您可以获得答案的链接@DhavalkumarSolanki,我已经读过这篇文章了。我使用了isInEditMode()
,但它不起作用!对不起,误会了
this.addView(rl);