我想在屏幕顶部显示横幅,但是有问题。在我的代码中(这不是我的代码,因为我在学习过程中)我有两个选择。在屏幕底部的屏幕下方或上方显示横幅。
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
,广告仍会显示在底部:(
任何人都可以帮我这个吗?非常感谢!
inLayout代码:
// 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();
答案 0 :(得分:1)
首先,我认为此代码整体View
是一个RelativeLayout
,其LinearLayout
包含其他View
个..第一个代码将您的addz与父级的底部和第二个代码将它与LinearLayout
视图容器中的容器底部对齐 - (如果我有意义,则为idk)
但是您的初始编辑是有效的,因为它相对于父对象进行了对齐,而您的第二次编辑则重新定义了addview在其容器中的位置..所以要在其他代码中更改 (假设我的逻辑是正确的)
//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();
}
}
});
}
} 对于相对的布局,你可以做对齐父母的东西