2016-12-20 38 views
0

enter image description hereAdMob的NativeExpressAdView在自定義不顯示查看的Android

我希望在退出對話框NativeExpressAdView。退出對話框是在app中創建的自定義對話框自定義對話框的代碼如下。

public void onBackPressed() { 
    System.out.println("IN ON BACK PRESSED:"); 
    final Dialog dialog = new Dialog(SplashActivity.this); 
    dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON); 
    dialog.setContentView(R.layout.exit_dialog); 

    LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View main = inflater.inflate(R.layout.exit_dialog, null); 
    dialog.setContentView(main); 
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 

    LinearLayout linear = (LinearLayout)main.findViewById(R.id.layoutAdd); 

    NativeExpressAdView mNativeExpressAdView = new NativeExpressAdView(this); 
    mNativeExpressAdView.setAdSize(new AdSize(AdSize.FULL_WIDTH, AdSize.AUTO_HEIGHT)); 
    mNativeExpressAdView.setAdUnitId("ca-app-pub-8410399846074282/9976822052"); 
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder(); 

    linear.addView(mNativeExpressAdView); 
    mNativeExpressAdView.loadAd(adRequestBuilder.build()); 

    dialog.setTitle("Title..."); 

    // set the custom dialog components - text, image and button 
    TextView text = (TextView) dialog.findViewById(R.id.dialog_text); 
    text.setText("Are you sure you wants to exit ?? " + R.string.app_name); 


    Button dialogButton = (Button) dialog.findViewById(R.id.btnCancel); 
    // if button is clicked, close the custom dialog 
    dialogButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      dialog.dismiss(); 
     } 
    }); 

    Button btnRateus = (Button) dialog.findViewById(R.id.btnRateus); 
    // if button is clicked, close the custom dialog 
    btnRateus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName()); 
      Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); 
      // To count with Play market backstack, After pressing back button, 
      // to taken back to our application, we need to add following flags to intent. 
      goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | 
        Intent.FLAG_ACTIVITY_NEW_DOCUMENT | 
        Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
      try { 
       startActivity(goToMarket); 
      } catch (ActivityNotFoundException e) { 
       startActivity(new Intent(Intent.ACTION_VIEW, 
         Uri.parse("http://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName()))); 
      } 
     } 
    }); 

    Button btnConfirm = (Button) dialog.findViewById(R.id.btnConfirm); 
    // if button is clicked, close the custom dialog 
    btnConfirm.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      finish(); 
     } 
    }); 
    dialog.show(); 
} 

它不直接工作,所以我試圖在Linearlayout中膨脹NativeExpressAdview,但它不顯示在linearlayout中。請幫助我......先謝謝你了!

回答

0

爲官方原生廣告Express文檔here在mentionned這是由於AUTO_HEIGHT屬性:

在這個時候,AUTO_HEIGHT常數和流體廣告大小不應與原生廣告快速使用。

0

與此

 // Load an ad into the AdMob banner view. 
     NativeExpressAdView adView = (NativeExpressAdView) main.findViewById(R.id.adView); 

     AdRequest request = new AdRequest.Builder().build(); 
     adView.loadAd(request); 

替換此代碼在您的代碼

NativeExpressAdView mNativeExpressAdView = new NativeExpressAdView(this); 
    mNativeExpressAdView.setAdSize(new AdSize(AdSize.FULL_WIDTH, AdSize.AUTO_HEIGHT)); 
    mNativeExpressAdView.setAdUnitId("ca-app-pub-8410399846074282/9976822052"); 
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder(); 

    linear.addView(mNativeExpressAdView); 
    mNativeExpressAdView.loadAd(adRequestBuilder.build()); 

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dp"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Are You Sure to Exit?" 
      android:id="@+id/dialog_text" 
      android:textSize="18dp" 
      android:textStyle="bold" 
      android:gravity="center_horizontal" /> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="10dp"> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="30dp" 
       android:text="Rate Us!" 
       android:id="@+id/btnRateus" 
       android:layout_weight="1" 
       style="@style/btnNew" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="30dp" 
       android:text="Cancel" 
       android:id="@+id/btnCancel" 
       android:layout_weight="1" 
       android:layout_marginLeft="10dp" 
       style="@style/btnNew" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="30dp" 
       android:text="Exit" 
       android:id="@+id/btnConfirm" 
       android:layout_weight="1" 
       android:layout_marginLeft="10dp" 
       style="@style/btnNew" /> 
     </LinearLayout> 
    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/relAds" 
     android:background="#ffffff" 
     android:gravity="bottom|center_horizontal"> 

     <com.google.android.gms.ads.NativeExpressAdView 
      android:id="@+id/adView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      ads:adUnitId="ca-app-pub-3940256099942544/2177258514" 
      ads:adSize="280x250"> 
     </com.google.android.gms.ads.NativeExpressAdView> 

    </RelativeLayout> 
</LinearLayout>