简体   繁体   中英

Align parent layout above its child view through code Android

    main_layout = (RelativeLayout) findViewById(R.id.main_layout_1st_screen);
    AdView adView = new AdView(this);
    adView.setAdUnitId("AD_ID");
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setId(660022451);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    adView.setLayoutParams(params);
    main_layout.addView(adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
    adView.setAdListener(new AdListener() {
        public void onAdLoaded() {

        }
    });

The above code works fine but since the adView is aligned bottom, the parent(main_layout) contents are blocked by the AdView. I want to align the parent(main_layout) above adView. any help ? Thanks in advance.

The easiest way would be to have a LinearLayout with orientation 'vertical' in which @+id/main_layout_1st_screen is first, and your AdView is second.

Why are you doing this in code? It's easy to just define the AdView in the layout xml as well (replace my webview with your RelativeLayout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff000000">

    <RelativeLayout
        android:id="@+id/main_layout_1st_screen"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="fill_parent">

    </RelativeLayout>


    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"

        android:id="@+id/about_ad"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_height="wrap_content"
        ads:adSize="@string/adsize"
        ads:adUnitId="@string/admobid"/>
</LinearLayout>

Fixed my self. I changed parent Relativelayout to LinearLayout with vertical orientation in XML and added the adView as child of LinearLayout as below code, then it worked awesome. (Note in XML, give Linearlayout child weight=1 and height=0dp)

    main_layout = (LinearLayout) findViewById(R.id.main_layout_1st_screen);
    final AdView adView = new AdView(this);
    adView.setAdUnitId("AdID");
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setId(660022451);
    final LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
    adView.setAdListener(new AdListener() {
        public void onAdLoaded() {
            main_layout.addView(adView, param);

        }
    });

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