缺少必需的XML属性adSize

时间:2016-05-22 08:45:55

标签: fragment admob adview

嘿,我读了很多主题,但我不知道自己做错了什么。当我包括

    ads:adSize="BANNER"
    ads:adUnitId="@string/admob"

它工作正常,但我想以编程方式包含admob代码

Start.java

public class Start extends _fragment {
    public Start() { super(); this.context = M.context; }

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.site_reklama, container,false);

        AdView adView = (AdView) view.findViewById(R.id.adView);
        adView.setAdUnitId(M.admob);
        adView.setAdSize(AdSize.BANNER);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        return view;
    }
}

site_reklama.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"            
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

<include layout="@layout/site_start"/>

<com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true">
</com.google.android.gms.ads.AdView>    

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

答案:

public class Start extends _fragment {

    public Start() { super(); this.context = M.context; }

    private static AdView adView;

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.site_reklama, container,false);
            adView = new AdView(getActivity());
            adView.setAdUnitId(M.admob);
            adView.setAdSize(AdSize.BANNER);  
            RelativeLayout layout = (RelativeLayout)view.findViewById(R.id.rr);
            layout.addView(adView);
            AdRequest adRequest = new AdRequest.Builder().build();   
            adView.loadAd(adRequest);

        return view;
    }

}

的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"            
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rr"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<include layout="@layout/site_start"/>

</RelativeLayout>