gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:16:51 27 4
gpt4 key购买 nike

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

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

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

最佳答案

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

关于java - 使用 no-xml 相对布局在屏幕底部的 Android 中定位 AdMob 横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24900725/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com