我正在尝试创建一些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。
这样可以吗?
谢谢