直播源码开发,各种常见的广告形式

直播源码开发,各种常见的广告形式

一.横幅式广告

编程方式创建广告布局

 

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/adMobView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
    private AdView mAdView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //初始化广告
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
        
        //用编程的方式创建布局加载读取广告
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        mAdView = new AdView(this);
        mAdView.setAdSize(AdSize.BANNER);
        //mAdView.setAdSize(new AdSize(300,50));
        mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111(测试横幅广告ID)");
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        layout.addView(mAdView);
        mAdView.loadAd(adRequestBuilder.build());
        setContentView(layout);
//广告事件
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                //广告完成加载时要执行的代码。
                Toast.makeText(getApplicationContext(), "Admob广告加载成功!", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onAdFailedToLoad(LoadAdError adError) {
                // 当广告请求失败时要执行的代码。
                Toast.makeText(getApplicationContext(), "Admob广告加载失败!", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onAdOpened() {
                // 当广告打开叠加层时要执行的代码
                // 覆盖屏幕。
            }
            @Override
            public void onAdClicked() {
                // 当用户点击广告时执行的代码。
            }
            @Override
            public void onAdClosed() {
                // 用户即将返回时执行的代码
                // 点击广告后进入应用程序。
            }
        });
}

二.插页广告

 

activity_two.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TwoActivity"
    tools:ignore="MergeRootFrame">
    <TextView
        android:id="@+id/game_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="@string/impossible_game"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/game_title"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <Button
        android:id="@+id/retry_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="重试"
        app:backgroundTint="#009688" />
</RelativeLayout>

 

以上就是直播源码开发,各种常见的广告形式, 更多内容欢迎关注之后的文章

 

posted @ 2022-08-03 14:19  云豹科技-苏凌霄  阅读(138)  评论(0)    收藏  举报