从xml中包含一个LinearLayout

3

我正在尝试创建一些Android视图类(它们只是对在XML文件中定义的布局进行包装的类)。以下是否正确:

public class MyViewWrapper extends LinearLayout {
    private TextView mTextView;

    public MyViewWrapper(Context context) {
        super(context);
    }

    public constructUI() {
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.myview, this);

        mTextView = (TextView)findViewById(R.id.myview_textview);
    }
}

所以想法就是我可以像这样构建我的视图,并在里面添加逻辑以修改它们的子视图等。布局如下:

<LinearLayout>
   <TextView />
</LinearLayout>

看起来我会得到一个额外的不必要的LinearLayout。包装类本身就是一个LinearLayout,然后它将附加来自xml文件的内部LinearLayout。

这样可以吗?

谢谢

1个回答

2
你可以尝试将布局文件中的替换为。我最近没有尝试过这个方法,上次尝试时好像遇到了问题,但理论上应该能够达到目的。基本上意味着“将我的所有子元素直接放入我被填充到的任何容器中”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接