【问题标题】:DrawerLayout with Admob ad带有 Admob 广告的 DrawerLayout
【发布时间】:2013-06-17 12:37:26
【问题描述】:

我在我的应用程序中使用 Navigation-Drawer。因此,我使用 navigation_drawer 布局创建了单个活动。每当用户从导航抽屉菜单中选择菜单选项时,我都会使用片段来更改主要内容区域(@+id/content_frame)数据.现在我的问题是我想在每个屏幕上显示 admob 广告。我可以使用片段来初始化广告布局并将其膨胀到 FrameLayout 中,但我认为这不是一个好的选择。他们有什么方法可以使用那个单一的活动来初始化广告?

navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- The main content view -->

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

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/side_navigation_background"
        android:cacheColorHint="#00000000"
        android:choiceMode="singleChoice"
        android:divider="@color/side_navigation_list_divider_color"
        android:dividerHeight="1dp" />

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

【问题讨论】:

    标签: android android-fragments navigation-drawer


    【解决方案1】:

    您可以在 DrawerLayout 中使用任何布局。在下面显示的 .xml 中,您可以在单个 Activity(使用 RelativeLayout)中切换片段,并在该 Activity 底部使用 admob 广告。

     <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" >
    
            <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"/>
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

      【解决方案2】:

      即使片段包含在抽屉布局中,也可以使用相对布局添加广告视图

      <RelativeLayout
              android:id="@+id/relative_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >
          <include
              layout="@layout/app_bar_main"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
      
              <com.google.android.gms.ads.AdView
                  android:id="@+id/adView"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true"
                  ads:adSize="BANNER"
                  ads:adUnitId="@string/banner_ad_unit_id"/>
          </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多