Set gravity for LinearLayout programmatically

橙三吉。 提交于 2020-01-02 03:23:08

问题


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 :(


回答1:


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

    }



回答2:


Use setGravity in LinearLayout



来源:https://stackoverflow.com/questions/25223374/set-gravity-for-linearlayout-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!