Andriod Studio Admob横幅广告和插页式广告

时间:2020-07-02 07:45:02

标签: android-studio admob ads banner adview

这是我的MainActivity。

FrameLayout simpleFrameLayout;
TabLayout tabLayout;
VideoView video;
Button btn_show;
InterstitialAd interstitialAd;
AdView adView1,adView2;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final LayoutInflater factory = getLayoutInflater();

    final View textEntryView = factory.inflate(R.layout.fragment_third, null);


    btn_show = (Button)textEntryView.findViewById(R.id.bt_show);
    adView1= (AdView)textEntryView.findViewById(R.id.ad_view);
    adView2= (AdView)textEntryView.findViewById(R.id.ad_view2);

    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

    AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("My phones IMEI").build();
    adView1.loadAd(adRequest);
    adView2.loadAd(adRequest);


    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    interstitialAd.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("My Phones IMEI").build());


    btn_show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            interstitialAd.show();
        }
    });

    // get the reference of FrameLayout and TabLayout
    simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
    tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
    // Create a new Tab named "First"
    TabLayout.Tab firstTab = tabLayout.newTab();
    firstTab.setText("Apps"); // set the Text for the first Tab
    firstTab.setIcon(R.drawable.app); // set an icon for the
    // first tab
    tabLayout.addTab(firstTab); // add  the tab at in the TabLayout
    // Create a new Tab named "Second"
    TabLayout.Tab secondTab = tabLayout.newTab();
    secondTab.setText("Products"); // set the Text for the second Tab
    secondTab.setIcon(R.drawable.company); // set an icon for the second tab
    tabLayout.addTab(secondTab); // add  the tab  in the TabLayout
    // Create a new Tab named "Third"
    TabLayout.Tab thirdTab = tabLayout.newTab();
    thirdTab.setText("Donate"); // set the Text for the first Tab
    thirdTab.setIcon(R.drawable.donation); // set an icon for the first tab
    tabLayout.addTab(thirdTab); // add  the tab at in the TabLayout


    // perform setOnTabSelectedListener event on TabLayout
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            // get the current selected tab's position and replace the fragment accordingly
            Fragment fragment = null;
            switch (tab.getPosition()) {
                case 0:
                    fragment = new FirstFragment();
                    break;
                case 1:
                    fragment = new SecondFragment();
                    break;
                case 2:
                    fragment = new ThirdFragment();
                    break;
            }
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.simpleFrameLayout, fragment);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.commit();
        }



        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

这是我的XML。按钮和adView与MainActivity不在同一页面上,但在放大布局后似乎可以使用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rellay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00BCD4"
    tools:context="info.androidhive.materialtabs.fragments.OneFragment">

    <Button
        android:id="@+id/bt_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Show Ad"
        android:textAllCaps="false" />
    <com.google.android.gms.ads.AdView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ad_view"
        android:layout_centerHorizontal="true"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>


    <com.google.android.gms.ads.AdView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ad_view2"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>


</RelativeLayout>

我要了

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

在《 Andriod清单》中。

我遵循了许多教程,并尝试丢失各种选择。 我为我添加了一个测试ID设备,我使用了Google给定AdMob测试ID的设备,我确保已完成了您可以做的一切,但是由于我是编码的初学者,所以我一无所知,而且我肯定错过了一些东西重要的是,这使我无法在自己的应用上展示广告。我将不胜感激,如果您知道出了什么问题并可以帮助我解决问题,这对我来说意义重大。

0 个答案:

没有答案