Set gravity for LinearLayout programmatically

2019-04-28 04:42发布

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.

I dont have any .xml file with LinearLayout/FrameLayout tags.

Here is all 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);
        }

    });   
}
}

I really dont know what the problem is. I spent all day to fint it, but without any solution :(

2条回答
爷的心禁止访问
2楼-- · 2019-04-28 04:49

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.

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

    }
查看更多
男人必须洒脱
3楼-- · 2019-04-28 04:49
登录 后发表回答