使用模板设置布局
您可以使用标准布局模板:该模板包括所有视觉广告资产的预配置设置,但可以进行自定义。
代码示例
Kotlin
Java
private fun showAd(nativeAd: NativeAd) {
val bannerAppearance = createNativeBannerViewAppearance()
binding.nativeBanner.applyAppearance(bannerAppearance)
binding.nativeBanner.setAd(nativeAd)
}
private fun createNativeBannerViewAppearance(): NativeTemplateAppearance {
return NativeTemplateAppearance.Builder()
.withBannerAppearance(
BannerAppearance.Builder()
.setBorderColor(Color.YELLOW).build()
)
.withCallToActionAppearance(
ButtonAppearance.Builder()
.setTextAppearance(
TextAppearance.Builder()
.setTextColor(Color.BLUE)
.setTextSize(14f).build()
)
.setNormalColor(Color.TRANSPARENT)
.setPressedColor(Color.GRAY)
.setBorderColor(Color.BLUE)
.setBorderWidth(1f).build()
)
.withImageAppearance(
ImageAppearance.Builder()
.setWidthConstraint(
SizeConstraint(
SizeConstraint.SizeConstraintType.FIXED,
60f
)
).build()
)
.withAgeAppearance(
TextAppearance.Builder()
.setTextColor(Color.GRAY)
.setTextSize(12f).build()
)
.withBodyAppearance(
TextAppearance.Builder()
.setTextColor(Color.GRAY)
.setTextSize(12f).build()
)
.withRatingAppearance(
RatingAppearance.Builder()
.setProgressStarColor(Color.CYAN).build()
)
.withTitleAppearance(
TextAppearance.Builder()
.setTextColor(Color.BLACK)
.setTextSize(14f).build()
)
.build()
}
private void showAd(@NonNull final NativeAd nativeAd) {
final NativeTemplateAppearance bannerAppearance = createNativeBannerViewAppearance();
mBinding.nativeBanner.applyAppearance(bannerAppearance);
mBinding.nativeBanner.setAd(nativeAd);
}
private NativeTemplateAppearance createNativeBannerViewAppearance() {
return new NativeTemplateAppearance.Builder()
.withBannerAppearance(
new BannerAppearance.Builder()
.setBorderColor(Color.YELLOW).build()
)
.withCallToActionAppearance(
new ButtonAppearance.Builder()
.setTextAppearance(
new TextAppearance.Builder()
.setTextColor(Color.BLUE)
.setTextSize(14f).build()
)
.setNormalColor(Color.TRANSPARENT)
.setPressedColor(Color.GRAY)
.setBorderColor(Color.BLUE)
.setBorderWidth(1f).build()
)
.withImageAppearance(
new ImageAppearance.Builder()
.setWidthConstraint(
new SizeConstraint(
SizeConstraint.SizeConstraintType.FIXED,
60f
)
).build()
)
.withAgeAppearance(
new TextAppearance.Builder()
.setTextColor(Color.GRAY)
.setTextSize(12f).build()
)
.withBodyAppearance(
new TextAppearance.Builder()
.setTextColor(Color.GRAY)
.setTextSize(12f).build()
)
.withRatingAppearance(
new RatingAppearance.Builder()
.setProgressStarColor(Color.CYAN).build()
)
.withTitleAppearance(
new TextAppearance.Builder()
.setTextColor(Color.BLACK)
.setTextSize(14f).build()
)
.build();
}
备注
创建您自己的基于模板的设计时,您不必为所有视觉元素进行首选设置。任何没有首选设置的元素都使用默认值进行配置。