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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:57:06  来源:igfitidea点击:

Android, add new view without XML Layout

javaandroidviewcontrols

提问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 中制作ActivityonCreate(Bundle),但我也在其他地方尝试过。

回答by lilbyrdie

You haven't actually told the system to drawanything 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 FrameLayouttag to my layout XML, then in the code call findViewById(), then I can use that FrameLayoutview to add the viewto (addView()).

当我以编程方式创建任意的意见,现有的内画layout,我通常添加FrameLayout标签到我的layout XML,然后在代码调用findViewById(),那么我就可以使用该FrameLayout视图来添加view至(addView())。

I hope that helps some without writing your code for you.

我希望在不为您编写代码的情况下对某些人有所帮助。