2011-08-11 59 views
0

我已创建自己的ViewGroup。我已经添加了一个小部件到视图中,我从Eclipse Design Editor中得到一个错误(我需要一个预览来获得更快/更好的开发)。自定义ViewGroup

public class MyViewGroup extends LinearLayout { 
    public MyViewGroup(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.addView(new EditText(context)); // The Error Line 
    } 
... 
} 

随着的LayoutParams鸵鸟政策改变什么:

this.addView(new Button(mcontext), 
      new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT)); 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<...MyViewGroup android:layout_height="match_parent" android:layout_width="match_parent" android:layout_margin="2dip" android:id="@+id/Token01"> </...MyViewGroup> 
</LinearLayout> 

我不看它在模拟器上。如果我删除“错误行”,设计器不会出错。什么是我的失败或无法呈现Eclipse Designer?但是为什么我在仿真器/设备上看不到它?我的目标是使用自定义视图和无XML的ViewGroup创建此部分。

我希望你能帮助我,对不起我的英语不好。

谢谢。

回答

0

如果我理解正确,代码中没有错误(运行时),但可视化编辑器无法正确显示它。
我曾经有过类似的问题(不记得),可以通过将可视化编辑器引擎更改为Honeycomb版本来解决它。
为了做到这一点,您首先需要安装一个Honeycomb SDK(API 11或更高版本)。然后,在右上角的可视化编辑器中,您可以选择它使用的SDK。更改为3.x.也许这也适用于你。

+0

感谢您的回答。该守则永远不会运行。可视化编辑器显示错误并且登录模拟器不显示EditText。 – Tim

0

我尝试了以下方法,它的工作原理与Android 2.3.1及更高版本一样。它不适用于2.2及更早的版本。

public class MyViewGroup extends LinearLayout { 
    public MyViewGroup(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     if (isInEditMode()) { 
     final EditText editText = new EditText(context); 
     editText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     addView(editText); 
     } 
    } 
} 

另请注意isInEditMode()的用法。

相关问题