此代码在没有admob部分的情况下运行良好。我无法在Android Studio的布局中添加admob。我如何在布局中添加admob,请看一下我的代码。帮我添加admob。代码没有错误,布局中没有广告。请有人建议我哪些部分需要编辑。谢谢
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="70dp"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/difficulty_margin"
android:layout_marginTop="30dp"
android:layout_marginRight="@dimen/difficulty_margin"
android:layout_marginBottom="30dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_1"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:layout_marginBottom="18dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_4"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_4"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/difficulty_margin"
android:layout_marginTop="30dp"
android:layout_marginRight="@dimen/difficulty_margin"
android:layout_marginBottom="30dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_2"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:layout_marginBottom="18dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_5"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_5"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/difficulty_margin"
android:layout_marginTop="30dp"
android:layout_marginRight="@dimen/difficulty_margin"
android:layout_marginBottom="30dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_3"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_3"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:layout_marginBottom="18dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_6"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_6"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="@dimen/margintoptendp"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_adUnitId" />
</LinearLayout>
DifficultySelectFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = LayoutInflater.from(Shared.context).inflate(R.layout.difficulty_select_fragment, container, false);
Theme theme = Shared.engine.getSelectedTheme();
DifficultyView difficulty1 = view.findViewById(R.id.select_difficulty_1);
difficulty1.setDifficulty(1, Memory.getHighStars(theme.id, 1));
setOnClick(difficulty1, 1);
/* FIrstFragment fIrstFragment = new FIrstFragment();
moveToFragment(fIrstFragment);*/
DifficultyView difficulty2 = view.findViewById(R.id.select_difficulty_2);
difficulty2.setDifficulty(2, Memory.getHighStars(theme.id, 2));
setOnClick(difficulty2, 2);
DifficultyView difficulty3 = view.findViewById(R.id.select_difficulty_3);
difficulty3.setDifficulty(3, Memory.getHighStars(theme.id, 3));
setOnClick(difficulty3, 3);
DifficultyView difficulty4 = view.findViewById(R.id.select_difficulty_4);
difficulty4.setDifficulty(4, Memory.getHighStars(theme.id, 4));
setOnClick(difficulty4, 4);
DifficultyView difficulty5 = view.findViewById(R.id.select_difficulty_5);
difficulty5.setDifficulty(5, Memory.getHighStars(theme.id, 5));
setOnClick(difficulty5, 5);
DifficultyView difficulty6 = view.findViewById(R.id.select_difficulty_6);
difficulty6.setDifficulty(6, Memory.getHighStars(theme.id, 6));
setOnClick(difficulty6, 6);
animate(difficulty1, difficulty2, difficulty3, difficulty4, difficulty5, difficulty6);
Typeface type = Typeface.createFromAsset(Shared.context.getAssets(), "fonts/grobold.ttf");
TextView text1 = view.findViewById(R.id.time_difficulty_1);
text1.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text1.setTypeface(type);
text1.setText(getBestTimeForStage(theme.id, 1));
TextView text2 = view.findViewById(R.id.time_difficulty_2);
text2.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text2.setTypeface(type);
text2.setText(getBestTimeForStage(theme.id, 2));
TextView text3 = view.findViewById(R.id.time_difficulty_3);
text3.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text3.setTypeface(type);
text3.setText(getBestTimeForStage(theme.id, 3));
TextView text4 = view.findViewById(R.id.time_difficulty_4);
text4.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text4.setTypeface(type);
text4.setText(getBestTimeForStage(theme.id, 4));
TextView text5 = view.findViewById(R.id.time_difficulty_5);
text5.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text5.setTypeface(type);
text5.setText(getBestTimeForStage(theme.id, 5));
TextView text6 = view.findViewById(R.id.time_difficulty_6);
text6.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text6.setTypeface(type);
text6.setText(getBestTimeForStage(theme.id, 6));
mAdview = view.findViewById( R.id.adView );
AdRequest adRequest = new AdRequest.Builder().tagForChildDirectedTreatment(true).build();
mAdview.loadAd( adRequest );
return view;
}
答案 0 :(得分:0)
回答您的问题
仅布局中没有广告
有2种可能的原因导致广告无法展示
1)如果您刚刚创建了广告暴民ID,那么您很可能不会收到任何广告。因为当您创建新的广告暴民ID时,有时需要向其ID提供一些广告
2)如果第一种情况无效,那么您已在4-5个小时前取得了ID,然后确保您的ID是正确的。误认为您没有使用错误的ID或任何错字。
答案 1 :(得分:0)
添加侦听器adview以查找错误。
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
Toast.makeText(this, "ad loaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
Toast.makeText(this, errorCode.toString, Toast.LENGTH_SHORT).show();
}
@Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
Toast.makeText(this, "Ad opened", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
Toast.makeText(this, "Ad left application", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
Toast.makeText(this, "ad closed", Toast.LENGTH_SHORT).show();
}
});
答案 2 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="70dp"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/difficulty_margin"
android:layout_marginTop="30dp"
android:layout_marginRight="@dimen/difficulty_margin"
android:layout_marginBottom="30dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_1"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:layout_marginBottom="18dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_4"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_4"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/difficulty_margin"
android:layout_marginTop="30dp"
android:layout_marginRight="@dimen/difficulty_margin"
android:layout_marginBottom="30dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_2"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:layout_marginBottom="18dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_5"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_5"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/difficulty_margin"
android:layout_marginTop="30dp"
android:layout_marginRight="@dimen/difficulty_margin"
android:layout_marginBottom="30dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_3"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_3"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:layout_marginBottom="18dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
<com.nayemuzzaman.kidsmemorize.ui.DifficultyView
android:id="@+id/select_difficulty_6"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1" />
<TextView
android:id="@+id/time_difficulty_6"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="-40dp"
android:background="@drawable/time_border"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="@dimen/margintoptendp"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_adUnitId" />
</RelativeLayout>
将xmlns:ads="http://schemas.android.com/apk/res-auto"
添加到您的根布局
在片段中:
private AdView mBannerAd;
mBannerAd = view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
mBannerAd.loadAd(adRequest);
添加依赖项
implementation 'com.google.android.gms:play-services-ads:11.6.0'
答案 3 :(得分:0)
替换片段中的admob部分。我使用此代码在InterstitialAd admob中进行了测试。
//For add of Admob app ID
MobileAds.initialize(getContext(),
"ca-app-pub-3940256099942544~3347511713");
mInterstitialAd = new InterstitialAd(getContext());
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
final AdRequest request = new AdRequest.Builder().build();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
mInterstitialAd.show();
}
@Override
public void onAdClosed() {
super.onAdClosed();
// mInterstitialAd.loadAd(request.build());
}
});
mInterstitialAd.loadAd( request );