将Admob添加到我的布局有什么问题?

时间:2015-10-24 22:26:50

标签: android android-layout admob

我想添加Admob,但我的布局存在以下问题:

我希望它出现在屏幕的底部。

这是我的activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Framelayout to display Fragments -->

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >



    </FrameLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/reklam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        android:clickable="false"
        android:contextClickable="false">
    </com.google.android.gms.ads.AdView>
    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>

</android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:0)

您希望在运行时在FrameLayout中添加AdView。试试这个:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Framelayout to display Fragments -->

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>


    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>

</android.support.v4.widget.DrawerLayout>

然后在你的活动onCreate:

mAdView = new AdView(this);
myAdView.setLayoutParams(new FrameLayout.LayoutParams(
                     FrameLayout.LayoutParams.WRAP_CONTENT, 
                     FrameLayout.LayoutParams.WRAP_CONTENT, 
                     Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
mAdView.setAdSize(AdSize.SMART_BANNER);
FrameLayout layout = (FrameLayout)findViewById(R.id.frame_container);
layout.addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111")
mAdView.loadAd(adRequest);

答案 1 :(得分:0)

尝试将这些参数放入Admob xml:

android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>