Facebook的Kotlin广告

时间:2018-08-22 09:55:20

标签: android kotlin facebook-android-sdk

我试图在用Kotlin编写的android应用上显示Facebook广告,但是每次我尝试添加显示错误的代码时,我都到处搜索但找不到Kotlin版本

    adView = new AdView(this, "YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_50);

  // Find the Ad Container
  LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);

  // Add the ad view to your activity layout
  adContainer.addView(adView);

  // Request an ad
  adView.loadAd();

1 个答案:

答案 0 :(得分:0)

Android Studio提供了从Java到Kotlin的转换工具。要找到它,请双击SHIFT,然后键入“将Java转换为Kotlin”。

adView = AdView(this, "YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_50)
// Find the Ad Container
val adContainer = findViewById<LinearLayout>(R.id.banner_container)
// Add the ad view to your activity layout
adContainer.addView(adView)
// Request an ad
adView.loadAd()

或使用kotlin扩展名

adView = AdView(this, "YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_50)
// Find the Ad Container and add the ad view to your activity layout
banner_container.addView(adView)
// Request an ad
adView.loadAd()