Android 在充气视图中充气视图
我正在使用扩展基本适配器的第三方。在base adapter中,我已经扩展了我的自定义布局,我希望在该自定义布局中扩展另一个布局 第三方视图是可刷卡,我正在尝试在卡之间显示facebook原生广告 这是我的密码 自定义_ads_cards.xmlAndroid 在充气视图中充气视图,android,android-layout,baseadapter,layout-inflater,Android,Android Layout,Baseadapter,Layout Inflater,我正在使用扩展基本适配器的第三方。在base adapter中,我已经扩展了我的自定义布局,我希望在该自定义布局中扩展另一个布局 第三方视图是可刷卡,我正在尝试在卡之间显示facebook原生广告 这是我的密码 自定义_ads_cards.xml <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://sc
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
<com.facebook.ads.NativeAdLayout
android:id="@+id/card_ad_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/txt_advertisement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/black_25"
android:fontFamily="@font/mr"
android:padding="8dp"
android:text="@string/advertisement"
android:textColor="@color/black"
android:textSize="@dimen/text_14" />
</androidx.cardview.widget.CardView>
我想显示custom_Ad_layout.xml的广告视图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_ads_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.facebook.ads.MediaView
android:id="@+id/card_ad_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/white"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="8dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.facebook.ads.AdIconView
android:id="@+id/native_ad_icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/native_ad_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:ellipsize="end"
android:lines="1"
android:textColor="@color/black"
android:textSize="@dimen/text_15" />
<TextView
android:id="@+id/native_ad_sponsored_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="5dp"
android:ellipsize="end"
android:lines="1"
android:textColor="@color/black_25"
android:textSize="@dimen/text_12" />
</LinearLayout>
<LinearLayout
android:id="@+id/ad_choices_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal" />
</LinearLayout>
<TextView
android:id="@+id/native_ad_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ellipsize="end"
android:gravity="center_vertical"
android:lines="2"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:textColor="@color/black_50"
android:textSize="@dimen/text_12" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:paddingStart="2dp"
android:paddingTop="3dp"
android:paddingEnd="2dp">
<TextView
android:id="@+id/native_ad_social_context"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/native_ad_call_to_action"
android:ellipsize="end"
android:gravity="start"
android:lines="1"
android:textColor="@color/black_25"
android:textSize="@dimen/text_12" />
<Button
android:id="@+id/native_ad_call_to_action"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="#4286F4"
android:textColor="@color/white"
android:textSize="@dimen/text_12"
android:visibility="visible" />
</RelativeLayout>
</LinearLayout>
下面是我的基本适配器的get-view方法
@Override
public View getView(final int position, View view, ViewGroup parent) {
View v = view;
v = LayoutInflater.from(myContext).inflate(R.layout.custom_ads_cards, null);
NativeAdLayout fbAdLayout = v.findViewById(R.id.card_ad_layout);
com.facebook.ads.NativeAd nativeAd = new com.facebook.ads.NativeAd(myContext, "YOUR_PLACEMENT_ID");
nativeAd.setAdListener(new NativeAdListener() {
@Override
public void onMediaDownloaded(Ad ad) {
// Native ad finished downloading all assets
Log.e("TAG", "CARD ad finished downloading all assets.");
}
@Override
public void onError(Ad ad, AdError adError) {
// Native ad failed to load
Log.e("TAG", "CARD ad failed to load: " + adError.getErrorMessage());
}
@Override
public void onAdLoaded(Ad ad) {
// Native ad is loaded and ready to be displayed
Log.d("TAG", "CARD ad is loaded and ready to be displayed!");
if (nativeAd != ad) {
return;
}
RelativeLayout fbAdView = (RelativeLayout) LayoutInflater.from(myContext).
inflate(R.layout.custom_ad_layout, fbAdLayout,false);
fbAdLayout.addView(fbAdView);
LinearLayout adChoicesContainer = fbAdView.findViewById(R.id.ad_choices_container);
AdOptionsView adOptionsView = new AdOptionsView(myContext, nativeAd, fbAdLayout);
adChoicesContainer.removeAllViews();
adChoicesContainer.addView(adOptionsView, 0);
// Create native UI using the ad metadata.
AdIconView nativeAdIcon = fbAdView.findViewById(R.id.native_ad_icon);
TextView nativeAdTitle = fbAView.findViewById(R.id.native_ad_title);
MediaView nativeAdMedia = fbAdView.findViewById(R.id.card_ad_media);
TextView nativeAdSocialContext = fbAdView.findViewById(R.id.native_ad_social_context);
TextView nativeAdBody = fbAdView.findViewById(R.id.native_ad_body);
TextView sponsoredLabel = fbAdView.findViewById(R.id.native_ad_sponsored_label);
Button nativeAdCallToAction = fbAdView.findViewById(R.id.native_ad_call_to_action);
// Set the Text.
nativeAdTitle.setText(nativeAd.getAdvertiserName());
nativeAdBody.setText(nativeAd.getAdBodyText());
nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
sponsoredLabel.setText(nativeAd.getSponsoredTranslation());
// Create a list of clickable views
List<View> clickableViews = new ArrayList<>();
clickableViews.add(nativeAdTitle);
clickableViews.add(nativeAdCallToAction);
// Register the Title and CTA button to listen for clicks.
nativeAd.registerViewForInteraction(
fbAdView,
nativeAdMedia,
nativeAdIcon,
clickableViews);
}
@Override
public void onAdClicked(Ad ad) {
// Native ad clicked
Log.d("TAG", "CARD ad clicked!");
}
@Override
public void onLoggingImpression(Ad ad) {
// Native ad impression
Log.d("TAG", "CARD ad impression logged!");
}
});
// Request an ad
nativeAd.loadAd();
return v;
}
@覆盖
公共视图getView(最终整数位置、视图视图、视图组父视图){
视图v=视图;
v=布局。从(myContext)开始,充气(R.layout.custom\u ads\u cards,空);
NativeAdLayout fbAdLayout=v.findViewById(R.id.card\u ad\u布局);
com.facebook.ads.NativeAd NativeAd=new com.facebook.ads.NativeAd(myContext,“您的位置ID”);
nativeAd.setAdListener(新的NativeAdListener(){
@凌驾
MediaDownLoaded上的公共无效(Ad){
//本机ad已完成下载所有资产
Log.e(“标签”,“卡片广告完成下载所有资产”);
}
@凌驾
公共无效者(广告、广告、广告){
//本机ad加载失败
Log.e(“标签”,“卡片广告加载失败:”+adError.getErrorMessage());
}
@凌驾
已加载的公共无效(Ad){
//本地广告已加载并准备好显示
Log.d(“标签”,“卡片广告已加载并准备好显示!”);
if(nativeAd!=ad){
返回;
}
RelativeLayout fbAdView=(RelativeLayout)LayoutFlater.from(myContext)。
充气(R.layout.custom_ad_layout,fbAdLayout,false);
fbaddlayout.addView(fbAdView);
LinearLayout adChoicesContainer=fbAdView.findViewById(R.id.ad\u choices\u container);
AdOptionsView AdOptionsView=新的AdOptionsView(myContext、nativeAd、fbAdLayout);
adChoicesContainer.removeAllViews();
adChoicesContainer.addView(adOptionsView,0);
//使用ad元数据创建本机UI。
AdIconView nativeAdIcon=fbAdView.findviewbyd(R.id.native\u ad\u图标);
TextView nativeAdTitle=fbAView.findviewbyd(R.id.native\u ad\u title);
MediaView nativeAdMedia=fbAdView.findViewById(R.id.card\u ad\u media);
TextView nativeAdSocialContext=fbAdView.findViewById(R.id.native\u ad\u social\u context);
TextView nativeAdBody=fbAdView.findviewbyd(R.id.native\u ad\u body);
TextView-sponsoredLabel=fbAdView.findviewbyd(R.id.native\u ad\u-sponsoredLabel);
Button nativeAddCallToAction=fbAdView.findViewById(R.id.native\u ad\u call\u to\u action);
//设置文本。
nativeAdTitle.setText(nativeAd.getAdvertiserName());
nativeAdBody.setText(nativeAd.getAdBodyText());
nativeAd.getAdSocialContext());
nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction()?View.VISIBLE:View.INVISIBLE);
nativeAd.getAdCallToAction());
sponsoredLabel.setText(nativeAd.getSponsoredTranslation());
//创建可单击视图的列表
List clickableViews=新建ArrayList();
clickableViews.add(NativeAddTitle);
单击可查看。添加(NativeAddCallToAction);
//注册标题和CTA按钮以收听点击。
nativeAd.registerViewForInteraction(
fbAdView,
国家媒体,
国家民主党,
可点击视图);
}
@凌驾
已单击公共无效(Ad){
//本地广告点击
Log.d(“标签”,“卡片广告点击!”);
}
@凌驾
公众虚无在线印象(Ad){
//本土广告印象
Log.d(“标签”,“卡片广告印象记录!”);
}
});
//请求广告
nativeAd.loadAd();
返回v;
}
你能发布一些代码吗?@RahulKumar我已经发布了代码nativeAd
的范围可能是问题所在。在返回v之前,您是否可以在末尾执行v.setTag(nativeAd)并查看是否有效问题到底是什么?你没有看到你的custom_ad_layout.xml
作为视图添加到NativeAdLayout
中吗?@RickSanchez是的,我看不到作为视图添加到NativeAdLayout.xml的NativeAdLayout中你能发布一些代码吗?@RahulKumar我发布了代码nativeAd
的范围可能是问题。在返回v之前,您是否可以在末尾执行v.setTag(nativeAd)并查看是否有效问题到底是什么?您是否没有看到您的自定义广告版面.xml
作为视图添加到NativeAddLayout
中?@RickSanchez是的,我无法看到自定义广告版面.xml作为视图添加到NativeAddLayout中