显示原生广告 OnBackpressed

Posted

技术标签:

【中文标题】显示原生广告 OnBackpressed【英文标题】:Show Native Ad OnBackpressed 【发布时间】:2021-01-21 16:35:01 【问题描述】:

我正在尝试在 onBackPressed 上展示 Facebook Audience Network 原生广告,我已经添加了所有布局,但我不知道如何实现它,这是我的 OnBackPressed 代码:

override fun onBackPressed() 
    AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Closing")
            .setMessage("Are you sure you want to Exit?")
            .setPositiveButton("Yes", DialogInterface.OnClickListener  dialog, which -> finish() )
            .setNegativeButton("No", null)
            .show()

我想在对话框上实施原生 Facebook Audience Network 广告,我已经创建了原生广告布局,如下所述:https://developers.facebook.com/docs/audience-network/guides/ad-formats/native/android

但是我真的不知道该怎么做,请你帮我解决这个问题

这是 OnBackPressed 对话框的屏幕截图

Screenshot : When i want native ad to be shown

谢谢

【问题讨论】:

【参考方案1】:

您可以使用.setView() 更改对话框布局。

看起来像这样:

public void onBackPressed() 
    AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Closing")
            .setView(R.layout.your_custom_layout)
            .setPositiveButton("Yes", DialogInterface.OnClickListener  dialog, which -> finish() )
            .setNegativeButton("No", null);

    AlertDialog dialog = builder.create();
    dialog.show();

    //Here you can use dialog.findViewById()

对不起,我不知道 Kotlin,但它会很相似。

【讨论】:

以上是关于显示原生广告 OnBackpressed的主要内容,如果未能解决你的问题,请参考以下文章