如何放置AdMob的屏幕(上图)的Andr​​oid动作条的顶部? [英] How to place AdMob at the top of the screen (above) Android Actionbar?

查看:171
本文介绍了如何放置AdMob的屏幕(上图)的Andr​​oid动作条的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试放置一个AdMob的广告在屏幕(上图)的机器人动作条的顶部。

I trying to place an AdMob ad at the top of the screen (above) the Android Actionbar.

  • 黄色=动作条
  • 红=含量
  • 在绿色= AdMob的

一个类似的问题我发现的是把一个广告底部与动作条的屏幕的显示出来。但到目前为止,我无法弄清楚如何将其放置在屏幕的顶部。

A similar question I found is to place an ad at the bottom of a screen with Actionbar showing up. But so far I couldn't figure out how to place it at the top of the screen.

我尝试了这种方式:

private AdView adView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // main view setup
    setContentView(R.layout.main);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // actionbar setup
    ActionBar actionBar = getActionBar();
    actionBar.setTitle("Search");
    actionBar.setDisplayHomeAsUpEnabled(true);

    final LinearLayout content = (LinearLayout) findViewById(android.R.id.content).getParent();

    // Create the adView
    adView = new AdView(this, AdSize.BANNER, "ID");

    // Lookup your LinearLayout assuming it’s been given
    // the attribute android:id="@+id/mainLayout"

    // Add the adView to it
    FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams) content.getLayoutParams();
    parm.gravity = Gravity.TOP;
    content.addView(adView);

    // Initiate a generic request to load it with an ad
    AdRequest adRequest = new AdRequest();
    adRequest.addTestDevice("TEST_DEVICE_ID");
    adView.loadAd(adRequest);
}

下面你可以看到我的应用程序的层次观众的一部分。视图ID /主是我的主要版面。 正如你可以在图片中看到的动作条有一个名为ID / action_bar_container的ID。在我的code以上我设法访问的FrameLayout内容,但我不能够访问action_bar_container视图。因为他们都在同一个层级(我认为)应该有可能以某种方式访问​​的ActionBar视图。

Below you can see a part of the hierarchy viewer of my app. The view "id/main" is my main layout. As you can see on the picture the ActionBar has a id called "id/action_bar_container". In my code above I managed to access the FrameLayout "content" but I wasn't able to access the action_bar_container view. Because they are on the same hierarchy level (I think) it should be possible to access that ActionBar view somehow.

但它没有工作:

不知道如何做到这一点或链接它如何工作或官方的说法,这是行不通的。

Any idea how to achieve that or a link how it could work or an official statement that it doesn't work.

推荐答案

您可以使用 getRootView()上的任何观点或 getWindow()。 getDecorView()获得 PhoneDecorView 。它有一个孩子 - 一个垂直的LinearLayout

You can usegetRootView() on any view orgetWindow().getDecorView() to get thePhoneDecorView. It has one child - a verticalLinearLayout.

添加您的布局有作为的第一个孩子。

Add your layout there as the first child.

请注意,这是一个坏,坏主意。从一个用户体验的角度来看,这是对所有设计指导一个可怕的打击。操作栏的必须在用于多种设计上的原因上。

Note that this is a bad, bad idea. From a user experience point of view, this is a horrific strike against all Design Guidelines. The action bar must be at the top for a multitude of design reasons.

二,这可能打破OEM为主题的装置。

Second, this might break on OEM-themed devices.

三,怎么样的景观片,你会真的有在顶部如此巨大吧?

Third, what about tablets in landscape, will you really have such a huge bar at the top?

在最后,是的,这是可能的,但它是一个可怕的想法。不这样做。

In conclusion, yes, it's possible but it's a horrible idea. Don't do it.

这篇关于如何放置AdMob的屏幕(上图)的Andr​​oid动作条的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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