Admob AdBanner加载会导致片段变形

时间:2015-05-11 08:53:45

标签: android android-fragments admob

在我的应用中,主屏幕上有4个Buttons,每个按钮都会打开一个新的Activity。每个Activity都包含FragmentFragment末尾有Admob横幅。每个Fragment的基本布局如下:     

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
//some stuff here
</LinearLayout>
<com.google.android.gms.ads.AdView
    android:id="@+id/adViewEncodeManual"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/ad_bottom_home_screen_banner">
</com.google.android.gms.ads.AdView>

HomescreenActivity使用TableLayout代替linearLayout。我在每个onActivityCreated内加载Fragment

   @Override
public void onActivityCreated( Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    if(localAdView == null)
    { // if prevents a loadRequest again when activity is resumed

        localAdView = (AdView) getActivity().findViewById(R.id.adViewHome);
        AdRequest adRequest = new AdRequest.Builder().addTestDevice("F0554446AEA73E5599774C945E9B99F1").build();
        localAdView.loadAd(adRequest);
    }

}

横幅广告在每个Activity上加载正常,但如果我一次又一次从1 Activity切换到另一个,我会得到随机损坏View的扭曲显示。当我点击任何地方时,它会得到修复。

问题在于横幅只是因为当我关闭互联网时,一切都很好。

任何建议人员?

Table_layout:

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context="com.apps.toffeesoft.easyqrReader.HomeScreenActivityFragment">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/adViewHome"
    android:layout_height="wrap_content">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/home_background"

    >
    <TableRow

        >
        <Button
            android:id="@+id/btnScan"
            android:elevation="@dimen/abc_control_corner_material"
            android:layout_weight="0.5"
            android:layout_width="0dp"
            android:layout_marginBottom="-6dp"
            android:layout_marginRight="-7dp"

            android:layout_height="wrap_content"
            android:text="@string/home_scan_heading"
            android:drawableTop="@drawable/ic_image_camera_alt" />

        <Button
            android:id="@+id/btnCreate"
            android:layout_weight="0.5"
            android:layout_width="0dp"
            android:layout_marginBottom="-6dp"
            android:layout_height="wrap_content"
            android:text="@string/home_create_plain_heading"
            android:drawableTop="@drawable/create_qr_plain"
            />
    </TableRow>
    <TableRow
        android:layout_width="match_parent">
        <Button
            android:id="@+id/btnCreateContact"
            android:layout_weight="0.5"
            android:layout_width="0dp"
            android:layout_marginRight="-7dp"
            android:layout_marginTop="-6dp"
            android:layout_height="wrap_content"
            android:text="@string/home_create_contact_qr_heading"
            android:drawableTop="@drawable/contact_image"
            />
        <Button
            android:id="@+id/btnQrHistory"
            android:layout_weight="0.5"
            android:layout_width="0dp"
            android:layout_marginTop="-6dp"
            android:layout_height="wrap_content"
            android:text="@string/qr_history"
            android:drawableTop="@drawable/history"
            />


    </TableRow>
</TableLayout>
</LinearLayout>
<!--
<com.google.android.gms.ads.AdView
    android:id="@+id/adViewHome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/ad_bottom_home_screen_banner">
</com.google.android.gms.ads.AdView>
-->
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

Fragment的所有常规布局的Fisrt与其View无关。您的ViewGroup LinearLayout包含_some stuff_has Height match_parent,看着那个,假设要填满整个屏幕你有位于父Admob View底部的ViewGroup RelativeLayout match_parent也是View所以是LinearLayout没有定位。

相反,如果您的View足以填满屏幕,或者让LinearLayout位于Admob <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_above="@+id/theidoftheadmob" android:layout_height="match_parent"> //some stuff here </LinearLayout> 成为ScrollView >

Comparable