如何使用viewpager和drawerlayout在页面底部显示admob广告

时间:2015-02-04 16:38:02

标签: android xml

我有一个带有drawerlayout和viewpager的活动我想在页面底部显示admob广告,以便当用户在viewpager中滑动时广告一直显示在底部。就像我们在任何新闻阅读应用中看到的那样 这是我的detail.xml        

    <!-- Framelayout to display Fragments -->

    <FrameLayout

        android:id="@+id/mainContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="5dp"
            android:paddingRight="5dp" >

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </android.support.v4.view.ViewPager>


        </RelativeLayout>
        <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                ads:adSize="BANNER"
                ads:adUnitId="id" />
    </FrameLayout>

    <ListView
        android:id="@+id/drawerlist"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#FFFFFF" />
    <!-- android:entries="@array/planets" -->

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

detail_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:minHeight="48dp"
        android:text="@string/title"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />

    <ScrollView
        android:id="@+id/sv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/title"
        android:layout_marginTop="10dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <WebView
                android:id="@+id/desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:scrollbarStyle="insideOverlay"
                android:text="@string/desc" />
        </LinearLayout>
    </ScrollView>



</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

在RelativeLayout中移动AdView并使用android:layout_alignParentBottom="true" 使其在所有视图的底部可见。

   <FrameLayout

    android:id="@+id/mainContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </android.support.v4.view.ViewPager>

         <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="id" />
    </RelativeLayout>

</FrameLayout>

答案 1 :(得分:0)

试试这段代码:

https://api.gdax.com/products/BTC-USD/candles?start=2015-05-07&amp;end=2015-05-08&amp;granularity=900

您还可以通过编程方式添加AdView:

<android.support.v4.widget.DrawerLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:background="@color/sColorPrimary"
            app:titleTextAppearance="@style/ToolbarTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <!-- activity view -->
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        app:context=".Form" >
            <FrameLayout
                android:id="@+id/frameLayoutMain"
                android:layout_above="@+id/relativeLayoutAds"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" />
            <RelativeLayout
                android:id="@+id/relativeLayoutAds"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:paddingTop="1dp"
                android:layout_alignParentBottom="true" >
                <com.google.android.gms.ads.AdView
                    android:id="@+id/adView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    ads:adSize="BANNER"
                    ads:adUnitId="id" />
            </RelativeLayout>
        </RelativeLayout>
    </LinearLayout>

    <!-- navigation drawer -->
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice" />

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

答案 2 :(得分:0)

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@+id/fragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/adView"
            android:background="@color/background" />

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="AD_UNIT_IT"
            ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
            android:gravity="bottom" />
    </RelativeLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"/>