【问题标题】:using space when ad is not loaded android未加载广告时使用空间android
【发布时间】:2015-08-01 00:03:02
【问题描述】:

我正在尝试在我的应用程序中添加 admob 广告,但是当我没有连接到互联网时,空间太小了......

我希望添加在加载后出现,直到那时广告空间应该被其余​​元素使用

怎么办???

我的活动文件如下 -

MainActivity.java `

package com.testapp.update1;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
private AdView adView;
GridView grid;
String[] icons = { "Historic Places", "Railway", "City Bus", "Movie",
        "Directory" };

int[] imageID = { R.drawable.ic_historic, R.drawable.ic_train,
        R.drawable.ic_bus, R.drawable.ic_movie, R.drawable.ic_call };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //adding advertisement 
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(CommanData.AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout1);
    layout.addView(adView);

    AdRequest adRequest = new AdRequest.Builder()
                                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                                .addTestDevice("4963BE445DBBB277")
                                .build();

    // Start loading the ad in the background.

    CustomGrid adapter = new CustomGrid(MainActivity.this, icons, imageID);
    grid = (GridView) findViewById(R.id.icongrid);
    grid.setAdapter(adapter);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this,
                    "You clicked at " + icons[position] + position,
                    Toast.LENGTH_SHORT).show();

            Intent web = new Intent();
            switch (position) {
            case 0:
                web.setClass(MainActivity.this, Places.class);
                startActivity(web);
                break;
            case 1:
                web.setClass(MainActivity.this, Railway.class);
                startActivity(web);
                break;
            case 2:
                web.setClass(MainActivity.this, BusOptions.class);
                startActivity(web);
                break;
            case 3:
                ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
                if(cd.isConnected()) {
                    web.setClass(MainActivity.this, Movies.class);
                    startActivity(web);
                } else {
                    showConnectivityDialog();
                }
                break;
            case 4:
                Intent intent = new Intent(MainActivity.this,
                        CallDirctory.class);
                startActivity(intent);
            default:
                break;
            }
        }
    });
    // to load add
    adView.loadAd(adRequest);

    // To rate application at google play
    // AppRater.showRateDialog(mContext, editor)
    AppRater.app_launched(MainActivity.this);
}

  @Override
  public void onResume() {
    super.onResume();
    if (adView != null) {
      adView.resume();
    }
  }

  @Override
  public void onPause() {
    if (adView != null) {
      adView.pause();
    }
    super.onPause();
  }

  /** Called before the activity is destroyed. */
  @Override
  public void onDestroy() {
    // Destroy the AdView.
    if (adView != null) {
      adView.destroy();
    }
    super.onDestroy();
  }


public void showConnectivityDialog() {
    AlertDialog.Builder notfoundDialog = new AlertDialog.Builder(MainActivity.this);
    notfoundDialog.setTitle("Not Connected")
            .setMessage("It seems you are not connected to internet")
            .setPositiveButton("Ok", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setIcon(R.drawable.ic_launcher)
            .show();
}
}

【问题讨论】:

    标签: java android android-layout admob


    【解决方案1】:

    保持可见性

    LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout1);
    

    即您最初将 Adview 添加到 View.GONE 的布局,因此它不会占用任何空间。

    现在将AdListener 添加到AdView 并在AdListeneronAdLoaded 方法中进行上述布局View.VISIBLE。这样在加载 Ad 之前,布局将永远不可见。

    例如。

    AdListener googleAdListener = new AdListener()
    {
    
        @Override
        public void onAdLoaded()
        {
            super.onAdLoaded();
            try
            {
                (LinearLayout) findViewById(R.id.LinearLayout1).setVisibility(View.VISIBLE);
            }
            catch (Exception e)
            {
                Logger.LogException(e);
            }
        }
    };
    
    adView.setAdListener(googleAdListener);
    

    注意:您必须使用最新的google-play-services-lib 而不是AdMobsSDK

    【讨论】:

    • 包含AdView的布局是任何布局的唯一子级吗?
    • 布局是父布局,即主容器
    • 你能发布你的布局吗?
    • 谢谢@Apoorv,你的代码真的很有帮助,只是对我的场景进行了一点改动就可以了
    【解决方案2】:

    就这样展示广告,克服没有网络时的空白 -->

    //In your activity //
    

    添加 Admob 广告

        AdRequest adRequest = new AdRequest.Builder().addTestDevice(" xxx").build();
        adView.loadAd(adRequest);
        //
        adView.setAdListener(new AdListener() {
    
            @Override
            public void onAdClosed() {
            }
    
            @Override
            public void onAdFailedToLoad(int error) {
                adView.setVisibility(View.GONE);
            }
    
            @Override
            public void onAdLeftApplication() {
            }
    
            @Override
            public void onAdOpened() {
            }
    
            @Override
            public void onAdLoaded() {
                adView.setVisibility(View.VISIBLE);
            }
        });
    

    在activity_main.xml中

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="60sp"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        android:background="#FBAC4F"
        ads:adUnitId="xxx"
        android:visibility="gone"
    
        />
    

    肯定会帮助所有开发者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      相关资源
      最近更新 更多