我已经创建了自己的ViewGroup。我已经在视图中添加了一个小部件,从Eclipse Design Editor得到一个错误(为了更快/更好的开发,我需要一个预览)。
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));The 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>我在模拟器上看不到。如果我删除"Error Line",设计器不会得到任何错误。我失败或者不能渲染Eclipse Designer的原因是什么?但是为什么我在Emulator/设备上看不到它?我的目标是创建此部分与自定义视图和ViewGroup没有XML。
我希望你能帮助我,并为我糟糕的英语道歉。
谢谢。
发布于 2011-08-12 05:52:39
如果我理解正确的话,代码中没有错误(当它运行时),但是可视化编辑器没有正确地呈现它。
我曾经遇到过类似的问题(不太记得了),可以通过将可视化编辑器引擎更改为Honeycomb版本来解决它。
为此,您首先需要安装Honeycomb SDK (API 11或更高版本)。然后,在右上角的可视化编辑器中,您可以选择它使用的SDK。更改为3.x。也许这也适用于你。
发布于 2011-08-12 06:09:00
我尝试了下面的方法,它在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()的用法。
https://stackoverflow.com/questions/7033223
复制相似问题