为的LinearLayout设置重力编程 [英] Set gravity for LinearLayout programmatically
问题描述
我跟制作新的AdMob插件统一的指令。广告显示正确,但我有问题的底部位置。他们出现在屏幕上方。我已经设置重力底部(为的FrameLayout),但横幅广告再次屏幕顶部显示。
I have followed an instruction for making new AdMob plugin for Unity. Ads show up correctly, but I have problem with bottom position. They show up at top of the screen. I have set gravity to bottom (for FrameLayout) but banner ads again show up at the top of screen.
我没有用的LinearLayout /的FrameLayout标签的任何.xml文件。
I dont have any .xml file with LinearLayout/FrameLayout tags.
下面是所有code:
public class playads {
private String adUnitID = "ca-app-pub-9578188175087140/5483276313";
private Activity activity; //Store the android main activity
private AdView adView; //The AdView we will display to the user
private LinearLayout layout; //The layout the AdView will sit on
public playads () {
activity = UnityPlayer.currentActivity;
activity.runOnUiThread(new Runnable() {
public void run(){
adView = new AdView(activity);
adView.setAdUnitId(adUnitID);
adView.setAdSize(AdSize.BANNER);
AdRequest request = new AdRequest.Builder().build();
adView.setAdListener(new AdListener() {
public void onAdLoaded() {
if(layout == null)
Log.d("null", "null");
{
activity.runOnUiThread(new Runnable() {
public void run(){
layout = new LinearLayout(activity);
layout.addView(adView);
//FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//p.gravity=Gravity.BOTTOM;
activity.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
((FrameLayout.LayoutParams)layout.getLayoutParams()).gravity = Gravity.BOTTOM;
}
});
}
}
}
);
adView.loadAd(request);
}
});
}
}
我真的不知道是什么问题。我花了一整天来FINT它,但没有任何解决办法:(
I really dont know what the problem is. I spent all day to fint it, but without any solution :(
推荐答案
记住重力
设置的景致儿童的定位,而 layout_gravity
设置其父项内的视图的定位。所以,你的情况,你要设置的LinearLayout,可以通过成员方法来完成的严重性。你也应该设定方向。
Remember that gravity
sets the positioning of a view's children, while layout_gravity
sets the positioning of a view within its parent. So in your case, you want to set the gravity of the LinearLayout, which can be done via member methods. You should also set the orientation.
您的run()方法看起来应该是这样的:
Your run() method should look something like:
public void run(){
layout = new LinearLayout(activity);
layout.setGravity(Gravity.BOTTOM);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(adView);
LinearLayout.LayoutParams lllp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
activity.addContentView(layout, lllp);
}
这篇关于为的LinearLayout设置重力编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!