【问题标题】:Android adMob Displaying in the Middle of ScreenAndroid adMob 显示在屏幕中间
【发布时间】:2012-01-07 11:42:08
【问题描述】:

我正在尝试在屏幕顶部显示 adMob 广告横幅。但是,如下所示,它显示在中间。有谁知道我该如何解决这个问题?

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    ndroid:id="@+id/linearLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:background="@drawable/main_bg" >

    <LinearLayout 
        android:id="@+id/linearLayout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:paddingTop="30dip"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/mnwvbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dip"
            android:background="@drawable/mnwvicon" />

        <ImageButton
            android:id="@+id/reportsbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/reportsicon" />

    </LinearLayout>

    <ImageButton
        android:id="@+id/mnwvshowbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/showicon"
        android:paddingTop="30dip" />

</LinearLayout>

这是我在代码中添加我的 LinearLayout 的 AdView:

    // Admob Banner ***********
    layout = (LinearLayout)findViewById(R.id.linearLayout);
    adView = new AdView(this, AdSize.BANNER, "a14ed285a63276d");
    layout.addView(adView);
    adView.loadAd(new AdRequest());   
    // *************************

这是我屏幕上显示的内容:

【问题讨论】:

  • 简短回答:@+id/linearLayout 在您的布局 xml 中使用了两次,尝试给内部命名,这应该可以解决您的问题。
  • @yorkw 抱歉,这是一个复制粘贴错误……在我的代码中实际上并不是这样。我编辑了我的问题并忘记将其从父 LinearLayout 中取出。不过谢谢!

标签: android xml layout admob


【解决方案1】:

我建议你使用相对布局。在以下布局中,根据您的布局,广告布局的 id 为“adLayout”:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    ndroid:id="@+id/linearLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_bg" >

    <LinearLayout 
        android:id="@+id/adLayout"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:layout_below="@id/adLayout"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:paddingTop="30dip"
        android:layout_height="wrap_content">
  [...]

【讨论】:

  • 谢谢!我最终不得不这样做,然后使用图形布局来设置图标的适当位置。不过谢谢!
【解决方案2】:

您要添加的 linearLayout 是父元素;整个屏幕。尝试向它添加一个子线性布局(另一个完全包含在主布局中的线性布局)并设置 android:gravity="bottom"。

这是一个没有按钮的示例。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_bg" >

    <LinearLayout 
        android:id="@+id/linearLayout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom" >

</LinearLayout>

或相对父级(样式仅包括基本尺寸。父级填充屏幕,子级包装广告。):

<RelativeLayout 
style="@style/FPFP">
<LinearLayout
    android:id="@+id/ads"
    style="@style/WCWC"
    android:layout_alignParentBottom="true"/>
</RelativeLayout>

【讨论】:

  • 我做了您要求的更改并更改了我的问题。我的问题中的图像就是我现在得到的。 (我知道 XML 没有 android:gravity="bottom",但它提供了几乎相同的东西。)
  • 当我过去这样做时,我实际上已将父元素设为相对布局,将广告容器设为线性布局,并使用“android:layout_alignParentBottom="true"”。我将在答案末尾添加该示例代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多