Android,添加没有 XML 布局的新视图 [英] Android, add new view without XML Layout

查看:22
本文介绍了Android,添加没有 XML 布局的新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些有经验的人的帮助.我希望这很容易.我只想在主程序视图上方显示新视图(创建它没有 XML 布局).是集成 AdMob.com 广告块:

I need a little help from people with expirience. I hope it's easy. I just want to show new View (creating it without XML layouts) above main program's view. It's to integrate AdMob.com ad block:

我写了这样的代码:

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!

附言我在 ActivityonCreate(Bundle) 中创建,但我也在其他位置尝试过.

P.S. I made in in Activity's onCreate(Bundle), but I tried in other locations too.

推荐答案

您实际上并没有告诉系统要绘制任何东西或在哪里绘制它.您需要查看关于您的活动的 setContentView(view) 文档.如果您试图让它在当前屏幕上绘制,请查看 Dialog(和 setContentView)的文档.

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).

当我以编程方式创建任意视图以在现有 layout 中绘制时,我通常会在我的 layout XML 中添加一个 FrameLayout 标记,然后在代码调用 findViewById() 中,然后我可以使用该 FrameLayout 视图将 view 添加到 (addView()).

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()).

我希望能帮助到一些无需为您编写代码的人.

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

这篇关于Android,添加没有 XML 布局的新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆