在自定义对话框AdMob广告(Admob ad on custom Dialog)

2019-07-04 18:33发布

我试图在应用实施AdMob广告。 我想要做的是广告上的自定义对话框的横幅广告。 我曾尝试一切,但无法找到解决方案。

我已做好对话的自定义XML。 当添加基于XML的AdMob,它不会运行。 所以,我想这样做编程。 但仍然不能使它发挥作用。

public void OnClickButton(View paramView)
  {
    int btn_id = paramView.getId();

       if (btn_id == R.id.hint_field)
       {
     //set up dialog
           if (hint != null && hint.length()>0) {
          final Dialog dialog = new Dialog(Activity.this);
          dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);

          //ad loading
          dialog.setContentView(R.layout.custom_dialog);
          RelativeLayout layout = (RelativeLayout)findViewById(R.id.dialog_l);
          layout.addView(ad);
          AdRequest r = new AdRequest();
          ad.loadAd(r);

          dialog.setTitle("Σχετικά με την λέξη :");
          dialog.setCancelable(true);

           //set up text
          TextView text = (TextView) dialog.findViewById(R.id.hint_text);
          text.setText(hint);



         //set up button
           Button button = (Button) dialog.findViewById(R.id.Button01);
           button.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
                   dialog.dismiss();
              }
          });

          // now that the dialog is set up, it's time to show it    
           dialog.show();
           dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
     }
   }   

点击一个按钮我显示自定义对话框。 在我的活动的onCreate方法我已经AdView的

  AdView  ad = new AdView(this, AdSize.BANNER, "a15xxxxxxxxxxx");

我终于找到一个NullPointerException:layout.addView(广告);

有任何想法吗 ?

提前致谢!

Answer 1:

问题解决了! 我只好先膨胀定制对话框界面。 下面的代码是工作代码!

 public void OnClickButton(View paramView)
  {
    int btn_id = paramView.getId();

       if (btn_id == R.id.hint_field)
       {
     //set up dialog
           if (hint != null && hint.length()>0) {
          final Dialog dialog = new Dialog(Activity.this, R.style.Theme_New);
          dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
          dialog.setContentView(R.layout.custom_dialog);

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

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

              ad = new AdView(this, AdSize.IAB_MRECT, "a15xxxxxxxx");


        AdRequest request = new AdRequest();
        Set<String> keywords = new HashSet<String>();
        keywords.add("game");
        request.setKeywords(keywords);

          linear.addView(ad);
          ad.loadAd(request);

          dialog.setTitle("Title :");
          dialog.setCancelable(true);

           //set up text
          TextView text = (TextView) dialog.findViewById(R.id.hint_text);
          text.setText(hint);


         //set up button
           Button button = (Button) dialog.findViewById(R.id.Button01);
           button.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
                   dialog.dismiss();
              }
          });

          // now that the dialog is set up, it's time to show it    
           lp.width = width;
           lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
           dialog.show();
           dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
           dialog.getWindow().setAttributes(lp);
           dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
           dialog.getWindow().getAttributes().height = LayoutParams.WRAP_CONTENT;
     }
   }    
}


文章来源: Admob ad on custom Dialog