使用no-xml相对布局在Android屏幕底部定位AdMob横幅

时间:2014-07-23 02:07:14

标签: java android admob positioning

下面的代码将AdMob横幅定位在视图的底部。但问题是我使用了LinearLayout并将横幅向上移动了75个像素。这很糟糕,因为横幅高度通常为50px,但在向上移动50px后,它只显示横幅的一部分。另一个问题是横幅高度不固定。

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(screenWidth, 2 * screenHeight - 75);


 // Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);

// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
    .build();

// Start loading the ad in the background.
adView.loadAd(adRequest);

// Adding full screen container
addContentView(adView, adParams);

我尝试使用RelativeLayout代替LinearLayout,如下所示:

RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT);


lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

..................
......................
addContentView(adView, lay);

但是这会在屏幕顶部显示横幅。我怎样才能把它带到底部?

3 个答案:

答案 0 :(得分:4)

AdRequest adRequest = new AdRequest.Builder()
  .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
  .build();

RelativeLayout relativeLayout = new RelativeLayout(this);
mFrameLayout.addView(relativeLayout);


RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(
   AdView.LayoutParams.WRAP_CONTENT,
   AdView.LayoutParams.WRAP_CONTENT);
 // align bottom
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
 // align center
adViewParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);


relativeLayout.addView(adView, adViewParams);


adView.loadAd(adRequest);
adView.setBackgroundColor(Color.BLACK);
adView.setBackgroundColor(0);

我的案例中mFrameLayout在cocos2d-x Cocos2dxActivity类中定义,定义如下:

    // FrameLayout
    ViewGroup.LayoutParams framelayout_params =
        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                   ViewGroup.LayoutParams.MATCH_PARENT);
    mFrameLayout = new FrameLayout(this);
    mFrameLayout.setLayoutParams(framelayout_params);

答案 1 :(得分:2)

如果您希望将AdView放置在屏幕底部,那么可以通过简单的方式将AdView包含在想要在LinearLayout中显示的内容并将其标记为android:Layout_weight="1"。这将确保它消耗AdView消耗的所有空间。

<!-- In your layout xml -->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

    <!-- NB make layout_weight=1 -->
    <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
    >

    .. your content goes here.

    </LinearLayout>

    <LinearLayout 
            android:id="@+id/adViewContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
    >

</LinearLayout>

现在在onCreate()

setContentView(r.layout.layouts.);

adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);

// Adding AdView to container
final ViewGroup adViewContainer = (ViewGroup) findViewById(R.id.adViewContainer);
adViewContainer.addView(adView);

// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
    .build();

// Start loading the ad in the background.
adView.loadAd(adRequest);

答案 2 :(得分:0)

在Android文档中,RelativeLayout.ALIGN_PARENT_BOTTOM是一个&#34;规则,用于将孩子的下边缘与其RelativeLayout父母的下边缘对齐,&#34;所以广告横幅将放置在RelativeLayout的底部,而不一定是屏幕的底部。要确保横幅位于屏幕底部,您需要扩展RelativeLayout以填充屏幕,并且您可以通过将RelativeLayout.LayoutParams.WRAP_CONTENT更改为RelativeLayout.LayoutParams.MATCH_PARENT来完成此操作。

编辑:这只会使横幅显示在RelativeLayout的父级底部,因此请确保RelativeLayout的底边位于屏幕底部。

至于使用LinearLayout,我建议使用RelativeLayout,因为它更通用,可容纳LinearLayouts,并且可能是解决此特定问题的最简单方法。

为了解决您对变量横幅高度的担忧,您设置的高度是与密度无关的像素(dp),因此它只会在设备之间变化,而不是在同一设备上多次启动应用程序之间(或者仿真器)。有关所有AdSizes及其解释,请参阅this page