0

我几乎尝试了一切。我认为在 XML 中放置广告并不安全,所以我想通过代码来做到这一点。这就是我用 XML 制作的方法

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.google.ads.AdView
        android:id="@+id/adViewContactsList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adUnitId="My Unit id"
        ads:adSize="BANNER"
        ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
        ads:loadAdOnCreate="true"/>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="200"
        android:layout_above="@id/adViewContactsList">

        <ListView
            android:id="@+id/contactsList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="#0000"
            android:listSelector="@drawable/listview_background_pressed"
            android:divider="@color/convListSep"
            android:dividerHeight="0.5dp"
            android:fadingEdge="none"
            android:scrollingCache="false" >
        </ListView>

        <View
            android:layout_width="fill_parent"
            android:layout_height="5dp"
            android:background="@drawable/background_shadow" />

    </FrameLayout>
</RelativeLayout>

每次我尝试通过它为广告制作页脚时,它都像 FrameLayout 一样工作(它涵盖了列表的底部)。这是我的代码(当然是在删除 XML 中的广告之后)

adView = new AdView(this, AdSize.BANNER, GateSMSApp.AD_ID);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layoutContactsList);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adView.setLayoutParams(params);

AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(adRequest);
layout.addView(adView);

RelativeLayout.LayoutParams frameLayoutParams = (RelativeLayout.LayoutParams) findViewById(R.id.frameLayoutList).getLayoutParams();
FrameLayout frameList = (FrameLayout) findViewById(R.id.frameLayoutList);
RelativeLayout.LayoutParams viewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
viewParams.addRule(RelativeLayout.ABOVE, adView.getId());
frameList.setLayoutParams(viewParams);

哈,明白了。

代码没问题,但在代码中创建的视图 id 是 -1 或不同的东西。重点是我所要做的就是更改 adView 的 ID:

adView.setId(123123);
4

0 回答 0