代码之家  ›  专栏  ›  技术社区  ›  Martin

我如何使用一个包含MapView的包装TabHost,该MapView下面还显示一个AdMob视图?

  •  6
  • Martin  · 技术社区  · 14 年前

    我对Android非常陌生(比如2天),但我对其他布局工具包很有经验。我试图在TabHost下面放置一个AdView,似乎我可以让TabWidget或AdView正确显示,但不能两者都显示。

    首先,这是我试图完成的ASCII艺术版本:

    --------------------------------------------
    | Tab 1               | Tab 2              |
    --------------------------------------------
    | MapView when tab 1 is selected           |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    --------------------------------------------
    | AdView that stays no matter what tab     |
    --------------------------------------------
    

    TabWidget或FrameLayout。我希望它在整个tabhost内容之下。

    这是我的布局 之前

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:myapp="http://schemas.android.com/apk/res/org.mbs3.android.ufcm"
     android:layout_height="fill_parent"
     android:layout_width="wrap_content"
     >
    
     <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <LinearLayout android:layout_width="fill_parent"
       android:id="@+id/home_layout" android:orientation="vertical"
       android:layout_height="fill_parent">
    
       <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    
       <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
    
        <RelativeLayout android:id="@+id/emptylayout1"
         android:orientation="vertical" android:layout_width="fill_parent"
         android:layout_height="fill_parent" />
        <!--
         programatically added tabs will appear here,
                                            one per activity
        -->
       </FrameLayout>
    
    
      </LinearLayout>
    
    
     </TabHost>
    </RelativeLayout>
    

    现在,我尝试了几个不同的技巧来添加AdView,比如 http://www.spencerelliott.ca/blogs/blog-android-menu

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:myapp="http://schemas.android.com/apk/res/org.mbs3.android.ufcm"
     android:layout_height="fill_parent" android:layout_width="wrap_content">
    
     <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <LinearLayout android:layout_width="fill_parent"
       android:id="@+id/home_layout" android:orientation="vertical"
       android:layout_height="fill_parent">
    
       <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    
       <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
    
        <RelativeLayout android:id="@+id/emptylayout1"
         android:orientation="vertical" android:layout_width="fill_parent"
         android:layout_height="fill_parent" />
        <!--
         programatically added tabs will appear here, usually one per
         activity
        -->
       </FrameLayout>
    
    
      </LinearLayout>
    
    
    
     </TabHost>
    
     <LinearLayout android:layout_width="fill_parent"
      android:id="@+id/ad_layout" android:layout_height="wrap_content"
      android:gravity="bottom" android:layout_alignParentBottom="true"
      android:layout_alignBottom="@+id/home_layout">
    
      <com.admob.android.ads.AdView android:id="@+id/ad"
       android:layout_width="fill_parent" android:layout_height="wrap_content"
       myapp:backgroundColor="#000000" myapp:primaryTextColor="#FFFFFF"
       myapp:secondaryTextColor="#CCCCCC"
       myapp:keywords="words" />
     </LinearLayout>
    
    </RelativeLayout>
    

    不幸的是,在那一个,我的地图视图在第一个选项卡上把缩放控件放在AdView的顶部。有没有我遗漏的简单布局?因为我可以让TabWidget为高度包装内容,或者AdView为高度包装内容,但是只有当它们超过@android:id/tabcontent。"

    谢谢

    1 回复  |  直到 13 年前
        1
  •  5
  •   mmathieum    14 年前

    <RelativeLayout>
        <LinearLayout 
            android:id="@+id/ad_id"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">
    
           <!-- the ad goes here -->
        </LinearLayout>
    
        <TabHost
            android:layout_above="@id/ad_id"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
          <!-- your view -->
        </TabHost>
    </RelativeLayout>
    

    在上半部分之前声明视图的下半部分有点违反直觉:-)