2013-06-17 43 views
11

Tôi đang sử dụng Ngăn điều hướng trong ứng dụng của mình. Tôi đã tạo hoạt động đơn lẻ với bố cục navigation_drawer. Tôi đang sử dụng các đoạn để thay đổi dữ liệu chính (@ + id/content_frame) bất cứ khi nào người dùng chọn tùy chọn menu từ menu ngăn điều hướng. Vấn đề của tôi là tôi muốn hiển thị quảng cáo admob trên mọi màn hình. Tôi có thể sử dụng các đoạn để khởi tạo bố cục quảng cáo và tăng cường nó vào FrameLayout nhưng tôi nghĩ đó không phải là lựa chọn tốt. sử dụng hoạt động đơn lẻ đó?DrawerLayout với quảng cáo Admob

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> 

Trả lời

32

Bạn có thể sử dụng bất kỳ cách bố trí bên trong DrawerLayout. Trong .xml được hiển thị bên dưới, bạn có thể chuyển đổi các đoạn trong hoạt động đơn lẻ (sử dụng RelativeLayout) với quảng cáo admob ở cuối hoạt động này.

<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> 
+1

cảm ơn rất hữu ích .. – Ahmed

0

Ngay cả khi một đoạn được bao gồm trong một DrawerLayout, adview có thể được bổ sung bằng RelativeLayout như

<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> 
Các vấn đề liên quan