以编程方式设置LinearLayout的重力

时间:2014-08-09 21:45:46

标签: android unity3d position admob android-linearlayout

我已经按照指示为Unity制作新的AdMob插件。广告正确显示,但我的底部位置有问题。它们显示在屏幕顶部。我已将重力设置为底部(对于FrameLayout),但横幅广告再次显示在屏幕顶部。

我没有任何包含LinearLayout / FrameLayout标记的.xml文件。

以下是所有代码:

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

    });   
}
}

我真的不知道问题是什么。我花了一整天的时间去尝试它,但没有任何解决方案:(

2 个答案:

答案 0 :(得分:20)

请记住gravity设置视图子项的位置,而layout_gravity设置视图在其父项中的位置。因此,在您的情况下,您需要设置LinearLayout的重力,这可以通过成员方法完成。您还应该设置方向。

你的run()方法应该类似于:

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

    }

答案 1 :(得分:0)

setGravity

中使用LinearLayout