简体   繁体   中英

Android - align center the RelativeLayout in FrameLayout

adView is the AdMob's View and I am adding it to another frame view like this:

adView = new AdView(this);

RelativeLayout relativeLayout = new RelativeLayout(this);
mFrameLayout.addView(relativeLayout);


RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(
   AdView.LayoutParams.WRAP_CONTENT,
   AdView.LayoutParams.WRAP_CONTENT);
   //important I use this to make it appear on the bottom
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);


relativeLayout.addView(adView, adViewParams);

It appear on the bottom of the frame layout, hence on the bottom of the screen (the frame layout is my screen). But is in not in the center, it is left aligned. How to align center the RelativeLayout in FrameLayout .

EIDT: Here is the FrameLayout in case you need it:

    // FrameLayout
    ViewGroup.LayoutParams framelayout_params =
        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                   ViewGroup.LayoutParams.MATCH_PARENT);
    mFrameLayout = new FrameLayout(this);
    mFrameLayout.setLayoutParams(framelayout_params);

I have done this an it worked:

adViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

Please confirm if it is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM