如何在我的布局上膨胀视图?
如何在布局上膨胀视图,其中布局上的所有 ui 元素都应该可见。 我正在尝试这条线,但它不起作用,我无法看到我的背景图像、按钮和其他图像。
MyView view1=new MyView(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view1= (MyView) inflater.inflate(R.layout.main, null);
请帮忙..提前致谢。
How to inflate View on layout, where all my ui elements which are on layout should be visible.
Am trying this lines but it is not working am not able to see my background image, button and other images.
MyView view1=new MyView(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view1= (MyView) inflater.inflate(R.layout.main, null);
Please help.. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于您正在做的事情,使用
膨胀后未获取视图的原因是您需要在当前视图中的某处添加该视图。目前,您的膨胀视图是孤立的。
For what you are doing, use
The reason for not getting your view after inflating is that you need to add that View in the current View somewhere. Currently your inflated view is orphaned.
这通常有效...
或者
然后将此视图添加到 contentView 中。
更新:
假设您需要在 main.xml 中画一条线,让我给您这个建议。不要通过扩展 View 类在运行时画线,而是像这样在 xml 中创建一个 View
希望它有所帮助。
this usually works...
or
and then add this view to the contentView.
Updated:
Assuming you have need to draw a line in your main.xml, let me give you this suggestion. instead of drawing line at runtime by extending View class, create a View in xml like this
hope it helps..
这终于奏效了。
This worked finally.