gpt4 book ai didi

java - 布局顶部的 Admob 横幅

转载 作者:行者123 更新时间:2023-11-30 02:22:25 29 4
gpt4 key购买 nike

我想在屏幕顶部显示横幅,但是有问题。在我的代码中(这不是我的代码,因为我正在学习过程中)我有两个选择。在屏幕底部的屏幕下方或上方显示横幅。

    public void showBanner(final boolean inLayout) {
//banner ad
if (BANNER_AD_UNIT_ID.length() > 0) {
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(BANNER_AD_UNIT_ID);

if (inLayout) {
//make ad visible on bottom of screen over surface
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
adView.setLayoutParams(params1);
} else {
//make ad visible on bottom of screen under surface
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
params.weight = 0;
adView.setLayoutParams(params);

}

如果我将 params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);BOTTOM 更改为 TOP,横幅将显示在屏幕顶部。但是如果我将 params.gravity = Gravity.BOTTOM;BOTTOM 更改为 TOP,广告仍然显示在底部 :(

谁能帮我解决这个问题?非常感谢!

布局代码:

// Start loading the ad in the background.
adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
public void onAdLoaded() {
View parent = (View) adView.getParent();
if (parent != null) {
if (!(parent.equals(layout) || parent.equals(linear_layout))) {
if (inLayout)
layout.addView(adView);
else
linear_layout.addView(adView);
recalculateScreen();
}
} else {
//add new banner ad to screen
if (inLayout)
layout.addView(adView);
else
linear_layout.addView(adView);
recalculateScreen();
}
}
});
}
}

//add relative layout to linear layout for banner ad mobility
linear_layout = new LinearLayout(this);
linear_layout.setOrientation(LinearLayout.VERTICAL);
linear_layout.addView(layout);

setContentView(linear_layout);
holder = surface.getHolder();

最佳答案

首先我认为这段代码整体 ViewRelativeLayout有一个 LinearLayout包含一些其他 View s .. 第一个代码将您的 addz 与父级的底部对齐,第二个代码将其与 LinearLayout 中其容器的底部对齐查看容器-(idk,如果我说得通的话)

但您的初始编辑有效,因为它相对于父级对齐,而您的第二次编辑重新定义了 addview 在其容器中的位置。因此要在 else 代码中更改它(假设我的逻辑是正确的)

   //make ad visible on bottom of screen under surface
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
params.weight = 0;
adView.setLayoutParams(params);
// my addition starts here
//inLayout is linearlayout, that was what i thought but no.
ViewGroup parent = (ViewGroup) adView.getParent(); // your linear layout
if(parent.getChildCount() >0){
View viewToTake = parent.getChildAt(0); // taking the first child element
parent.remove(parent.indexOfChild(adView)); // you get the index of the adview layout and remove it
parent.addView(adView,0); // adding it at the first position
parent.addView(viewToTake,parent.getChildCount());// adding the view we took out back into play, you can decide to add it as the
// second element since it was your initial first with parent.addView(viewToTake,1);

编辑 1复制粘贴这个并留下第一个..

adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
public void onAdLoaded() {
View parent = (View) adView.getParent();
if (parent != null) {
if (!(parent.equals(layout) || parent.equals(linear_layout))) {
if (inLayout)
layout.addView(adView);
else
linear_layout.addView(adView,0); // adding it to linearLayout first element, that's what the zero does
recalculateScreen();
}
} else {
//add new banner ad to screen
if (inLayout)
layout.addView(adView);// same here
else
linear_layout.addView(adView,0); //same here, goes to the top
recalculateScreen();
}
}
});
}

}对于相对布局,你可以做对齐 parent 的事情

关于java - 布局顶部的 Admob 横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28287562/

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