Java Android,添加没有 XML 布局的新视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1211559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android, add new view without XML Layout
提问by Vadim
I need a little help from people with expirience. I hope it's easy.
I just want to show new View (creating it withoutXML layouts
) above main program's view.
It's to integrate AdMob.com ad block:
我需要一些有经验的人的帮助。我希望这很容易。我只想在主程序的视图上方显示新的视图(在没有创建它的情况下XML layouts
)。集成 AdMob.com 广告块:
I wrote such code:
我写了这样的代码:
AdView ad = new AdView(this);
ad.layout(10, 10, 100, 100);
ad.setVisibility(View.VISIBLE);
ad.bringToFront();
ad.requestFocus();
ad.invalidate();
As you see - nothing helped, no window visible
What do I make incorrectly?
Thanks!
如您所见 - 没有任何帮助,没有窗口可见
我做错了什么?谢谢!
P.S. I made in in Activity
's onCreate(Bundle)
, but I tried in other locations too.
PS 我是在 in 中制作Activity
的onCreate(Bundle)
,但我也在其他地方尝试过。
回答by lilbyrdie
You haven't actually told the system to draw
anything or where to draw it. You'll want to look at the documentation for setContentView(view)
on your Activity. If you're trying to get this to draw over your current screen, look at the documentation for Dialog
(and setContentView
).
你实际上并没有告诉系统draw
任何东西或在哪里绘制它。您需要查看setContentView(view)
有关您的活动的文档。如果您试图将其绘制在当前屏幕上,请查看Dialog
(和setContentView
)的文档。
When I'm programmatically creating arbitrary views to draw within an existing layout
, I usually add a FrameLayout
tag to my layout XML
, then in the code call findViewById()
, then I can use that FrameLayout
view to add the view
to (addView()
).
当我以编程方式创建任意的意见,现有的内画layout
,我通常添加FrameLayout
标签到我的layout XML
,然后在代码调用findViewById()
,那么我就可以使用该FrameLayout
视图来添加view
至(addView()
)。
I hope that helps some without writing your code for you.
我希望在不为您编写代码的情况下对某些人有所帮助。