从 xml 布局文件创建自定义 View 类的最佳方法?
我在名为“myview.xml”的 xml 布局文件中定义了一个relativelayout,其中包含许多子视图(TextViews、ImageViews 等)。
我想创建一个名为“MyView”的 View 对象/类来表示此relativelayout,它允许我使用我将定义的方法更改子视图的属性,以便我可以以编程方式添加(和检索)MyView 的实例(来自)其他观点。
最好的方法是什么?目前,我正在创建一个名为“MyView”的类,它扩展了 FrameLayout 并在重写 FrameLayout 的三个构造函数中调用以下方法...
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.myview, null);
addView(view);
...这有效,但这意味着我所有的relativelayout都包含在framelayout中,因此我有一个额外的我的视图层次结构中的(不必要的)层...
I have a RelativeLayout defined in an xml layout file named 'myview.xml' which contains a number of sub-Views (TextViews, ImageViews etc).
I want to create a View object/class named 'MyView' that represents this RelativeLayout, which allows me to change the properties of the sub-Views using methods I will define and such that I can add (and retrieve) instances of MyView programmatically to (from) other Views.
What is the best way to do this? At the moment I am creating a class named 'MyView' which extends FrameLayout and in overriding FrameLayout's three constructors I call the following methods...
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.myview, null);
addView(view);
... This works but it means all my RelativeLayouts are contained within FrameLayouts and thus I have an extra (unnecessary) layer in my view hierarchy...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也做几乎同样的事情。但我发送“this”而不是 null,并且不将该视图添加到父视图中。
I do pretty much the same thing. But I send in 'this' rather than null and don't add that view to the parent view.