首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义ViewGroup

自定义ViewGroup
EN

Stack Overflow用户
提问于 2011-08-12 05:40:08
回答 2查看 1.6K关注 0票数 0

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

代码语言:javascript
复制
public class MyViewGroup extends LinearLayout {
    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.addView(new EditText(context));  // The Error Line
    }
...
}

使用LayoutParams不会改变任何东西:

代码语言:javascript
复制
this.addView(new Button(mcontext),
            new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));

The main.xml

代码语言:javascript
复制
<?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。

我希望你能帮助我,并为我糟糕的英语道歉。

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2011-08-12 05:52:39

如果我理解正确的话,代码中没有错误(当它运行时),但是可视化编辑器没有正确地呈现它。

我曾经遇到过类似的问题(不太记得了),可以通过将可视化编辑器引擎更改为Honeycomb版本来解决它。

为此,您首先需要安装Honeycomb SDK (API 11或更高版本)。然后,在右上角的可视化编辑器中,您可以选择它使用的SDK。更改为3.x。也许这也适用于你。

票数 0
EN

Stack Overflow用户

发布于 2011-08-12 06:09:00

我尝试了下面的方法,它在Android 2.3.1和更高版本中工作得很棒。它不适用于2.2及更早版本。

代码语言:javascript
复制
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()的用法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7033223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档