简体   繁体   中英

Android: adding ad at bottom and shrinking the other view

I'm pretty new to Android layouts, and i'm trying to add an AdView at the bottom of my main view, and move the main view up, shrinking its height. I took a code somewhere that makes the ad appear at bottom, however the application's height does not shrink: the Ad takes the bottom of the application. I must make it programatically, with no xmls.

This is the code i have:

  View mainView; // of type SurfaceView
  adView = new AdView(this);
  adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
  adView.setAdSize(AdSize.SMART_BANNER);
  mainLayout = new RelativeLayout(this);
  mainLayout.addView(mainView);
  RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  mainLayout.addView(adView, adParams);
  setContentView(mainLayout);      

Sometime later i show the adView:

  adView.setVisibility(View.VISIBLE);
  adView.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());

Thanks in advance.

I am fairly new to android as well, but it sounds like you need to add a layout align tag to the adView. That ought to push the application up and put the add at the bottom. I hope this helps a little bit!

its a simple one:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_weight="1.0"/>

<LinearLayout
    android:layout_height="60dp"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_height="wrap_content"
        android:text="add your ad here"
        android:layout_width="wrap_content"/>

</LinearLayout>

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