我以编程方式在每个布局的顶部添加广告。在LinearLayout中,一切正常,但在RelativeLayouts中,广告与内容重叠。
// activity
AdHandler.setAd(this, (RelativeLayout) findViewById(R.id.activity_bet), null);
// handler
public class AdHandler {
private static AdView adView;
public static void setAd(Context context, RelativeLayout relLayout, LinearLayout linLayout) {
// Create the adView.
adView = new AdView(context);
adView.setAdUnitId(context.getString(R.string.ad_unit_id));
adView.setAdSize(AdSize.BANNER);
// Add the adView to it.
if (relLayout != null) {
relLayout.addView(adView, 0);
}
else
linLayout.addView(adView, 0);
// Initiate a generic request.
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("034CB02678DF7E469581XXXXX").build();
// Load the adView with the ad request.
adView.loadAd(adRequest);
}
}
修改
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.myfirstapp.LoginActivity" >
<!-- Loading progress -->
<LinearLayout
android:id="@+id/loading_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp" />
<TextView
android:id="@+id/loading_status_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif-light"
android:text="@string/initial_loading_status_message"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<RelativeLayout
android:id="@+id/activity_bet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="@+id/adPlaceholder">
<RelativeLayout
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/quoteLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quote:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/quote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/quoteLabel"
android:layout_marginLeft="3dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/dateLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date:"
android:layout_below="@+id/quote"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/quote"
android:layout_toRightOf="@+id/dateLabel"
android:layout_marginLeft="3dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dateLabel"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView android:id="@+id/verified"
android:contentDescription="@string/verified"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/username"
android:src="@drawable/ic_accept"
android:layout_marginTop="4dp"
android:layout_marginLeft="2dp"
android:visibility="gone" />
<ImageView
android:id="@+id/avatar"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_below="@+id/username"
android:contentDescription="Avatar"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Profile"
android:layout_below="@+id/avatar"
android:layout_alignParentRight="true" />
<ProgressBar
android:id="@+id/subscribeLoading"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_below="@+id/avatar"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:visibility="gone" />
</RelativeLayout>
<TextView android:id="@+id/bet_listview_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#EE2C2C"
android:textSize="16sp"
android:padding="2dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_below="@+id/header"
android:text="@string/bet_listview_header" />
<ListView
android:id="@+id/bet_listview"
android:layout_below="@+id/bet_listview_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
</merge>
答案 0 :(得分:1)
我建议这样的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/adPlaceholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
</LinearLayout>
<RelativeLayout
android:id="@+id/rlOtherContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/adPlaceholder" >
</RelativeLayout>
</RelativeLayout>
LinearLayout
&#34; adPlaceholder&#34;在您向其添加横幅之前,它将保持为空。如果添加了横幅广告,请注意代表主要内容的RelativeLayout
的&#34; layout_below&#34; 属性。它会自动在横幅下面自动对齐,防止其重叠。
代码示例:
LinearLayout placeholder = (LinearLayout) findViewById(R.id.adPlaceholder);
placeholder.addView(AdView);
如果您想将横幅放在主要内容的RelativeLayout
下方,只需更改.xml文件中的顺序,然后使用&#34; layout_above&#34; 属性在RelativeLayout
中,而不是&#34; layout_below&#34;。