# 横幅广告

<div align="left"><figure><img src="/files/ycv7P48L4kDZT0tnr1SD" alt=""><figcaption></figcaption></figure></div>

横幅广告是一种在用户手机屏幕顶部或顶部等设定位置进行停留内容展示的广告方案，可进行定时刷新与自动更换广告内容，用户可与广告进行交互，达到长时间广告展示效果

当前指南展示用户如何在应用中如何集成横幅广告到应用中，以及相关使用与注意事项

## 前提

用户完成广告初始化步骤[入门指南](/copy-of-cn/jie-kou-wen-dang/android/kai-shi/ru-men-zhi-nan.md)

## 添加View到布局中

首先要展示横幅广告，可选择手动放置Adx3BannerAdView到你希望展示广告的`Activity` 或 `Fragment` 的布局中，最简单办法是将相应XML布局文件添加到对应XML文件中，以下可参考页面中添加XML的事例

```xml
 <io.adx3.library.view.Adx3BannerAdView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:Size="BANNER"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:unitId="UnitId" />
```

* **app:Size** - 当前属性配置设置为广告所需展示尺寸，系统默认提供常规尺寸样式，若您不想使用当前配置，可选择自定义样式，请参考以下[广告尺寸](#guang-gao-chi-cun-pei-zhi)部分，自定义广告尺寸可参考[自定义尺寸](#zi-ding-yi-guang-gao-wei-chi-cun)
* **app:unitId** - 当前广告位id，当前属性用于您横幅广告唯一广告位标识，不同类型广告采用不同广告位，具体广告位来源，请参考[广告配置](/copy-of-cn/jie-kou-wen-dang/android/kai-shi/ru-men-zhi-nan.md#zai-ssp-guan-li-hou-tai-shen-qing-sdk-ying-yong-yu-guang-gao-id)

{% hint style="info" %}
注意当前布局添加方式，广告实际展示位置由广告XML相关，具体位置等相关属性与页面配置绑定
{% endhint %}

## 直接添加view

您也可以选择直接添加View的方式，无需完成XML编写

```java
  Adx3dBannerAdView bannerView = new Adx3BannerAdView(getContext());
  bannerview.setAdSize(AdSize.BANNER);
  bannerview.setUnitId("UnitId");
```

{% hint style="danger" %}
警告：直接添加view与XML添加方式相同，必须设置广告Unitid与Size(均完成以上事例参数配置)
{% endhint %}

## 自定义广告位尺寸

控件创建后，针对广告进行尺寸内容配置，可自定义广告窗体大小，根据不同尺寸展示不同模版的广告窗体与内容格式，用户自定义尺寸可配置所需AdSize，如下所示

```java
 AdSize adSize = new AdSize(300,300);
```

## 广告加载

广告View添加完成后，进行广告加载，直接调用广告的loadAd方法完成加载，以下事例展示完整广告加载流程

```java
public class MainActivity extends AppCompatActivity {
    Adx3BannerAdView bannerview;

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

        //TODO use XML to Add AdView
        bannerview = findViewById(R.id.adView);
        
        //TODO use Create to Add AdView
        bannerview = new Adx3BannerAdView(getContext());
        bannerview.setAdSize(new AdSize(width,height));
        bannerview.setUnitId("UnitId");
        
        //TODO load the AD
        bannerview.loadAd();
    }
}
```

好了，至此广告添加与配置已完成，可自动展示横幅广告了

{% hint style="info" %}
注意，如果广告加载失败，在广告位配置了自动刷新的情况下，广告SDK会在失败或成功后自动进行广告位数据刷新，若未配置广告，用户可执行加载方法进行广告手动加载刷新，推荐完成广告位自动刷新配置
{% endhint %}

## 添加广告监听事件

实现横幅广告后，用户可添加回调方法到方法中，用于监控当前广告加载过程，如广告开启，广告关闭，广告加载完成等事件，可用于用户后续业务流程开发

```java
bannerView.setAdListener(new AdListener() {
            @Override
            public void show() {
            //Code to be executed when the ad has been showed int Screen
            }

            @Override
            public void loadFaild() {
            //Code to be executed when the ad load faild
            }

            @Override
            public void loadSucces() {
            //Code to be executed when the ad load success
            }

            @Override
            public void adClose() {
            //Code to be executed when the ad close by user
            }

            @Override
            public void adClick() {
            //Code to be executed when user click the ad
            }

            @Override
            public void adComplate() {
             //Code to be executed when the ad is finish   
            }

            @Override
            public void adSkip() {
            //Code to be executed when user skip the ad not wait for finish it
            }

            @Override
            public void clickUrl(String url) {
            //Code to be executed when user click the ad and tell the url to uesr
            }
        });
```

{% hint style="info" %}
广告监听方法，添加于loadAd方法前，否则可能导致监听缺失
{% endhint %}

## 操作回调方法描述与介绍

<table><thead><tr><th width="368.5">回调事件名称</th><th>回调事件描述</th></tr></thead><tbody><tr><td>show()</td><td>当广告成功展示到用户页面</td></tr><tr><td>loadFaild()</td><td>当广告加载失败或展示失败时，告知用户当前广告加载失败状态，用户可通过查看错误code编码查询当前失败原因</td></tr><tr><td>loadSuccess()</td><td>当广告数据加载成功后提示用户</td></tr><tr><td>adClose()</td><td>当广告展示完成后，用户手动或自动进行广告内容关闭与回收，通知用户当前广告流程完成</td></tr><tr><td>adClick()</td><td>当用户点击广告，默认启动系统浏览器完成广告链接展示，用户可根据事件返回相关内容做应用配套业务关联，如数据链接或页面跳转</td></tr><tr><td>adComplate()</td><td>当广告单次完成加载并展示后完整展示流程，可适用于用户监听广告性能监控</td></tr><tr><td>adSkip()</td><td>适用于用户对广告进行跳过操作，不进行广告完整播放业务的通知</td></tr></tbody></table>

## 广告尺寸配置

提供常用模版大小配置

| 大小配置         | 描述                     |
| ------------ | ---------------------- |
| 320 \* 50    | BANNER - 常用横幅广告        |
| 468 \* 60    | LARGE\_BANNER - 大型横幅广告 |
| 320 \* 100   | FULL\_BANNER - 填充横幅广告  |
| 自定义高度\*自定义宽度 | 用户自定广告位高宽              |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.adx3.io/copy-of-cn/jie-kou-wen-dang/android/guang-gao-ge-shi/heng-fu-guang-gao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
