【问题标题】:Ads cannot show properly, activity_main.xml广告无法正常展示,activity_main.xml
【发布时间】:2014-11-12 06:13:21
【问题描述】:

在LinearLayout中,我的添加无法显示,说没有足够的广告空间,当我将其更改为RelativeLayout时,广告可以显示,但是我保留在顶部的按钮不见了。请帮我。而且,如果可能的话,我想把我的添加放在底部。谢谢。

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="25"
        android:onClick="gotoPreviousImage"
        android:padding="10dip"
        android:text="&lt;" />

    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:onClick="setAsWallpaper"
        android:padding="10dip"
        android:text="Set as Background" />

    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="25"
        android:onClick="gotoNextImage"
        android:padding="10dip"
        android:text=">" />
</LinearLayout>

<ImageView
    android:id="@+id/backgroundPreview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="Preview of the wallpaper to choose"
    android:scaleType="fitXY" />

<com.google.android.gms.ads.AdView
    android:id="@+id/AdView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adUnitId="Unit_ID"
    ads:adSize="SMART_BANNER"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    />
</LinearLayout>

【问题讨论】:

  • 尝试为单个子组件赋予像 layout_weight 这样的权重
  • 你是如何显示这个 ImageView 的?我猜这个有 fill_parent 的高度和宽度。因此,请尝试将其删除一会儿,看看其他一切是否正常。
  • @vo12 不工作 :( 仍然是同样的错误“没有足够的空间来展示广告。需要 360x50 dp,但只有 360x0 dp。”
  • 伙计,你已经在第一个子线性布局、imageview 和 adview 上放置了垂直方向放置权重,并使高度为 0px
  • @SeijiDinzuala 给 Imageview 高度 wrap_parent

标签: android xml android-layout android-studio


【解决方案1】:
//Replace this code with your xml code and replay me  

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

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="25"
                android:onClick="gotoPreviousImage"
                android:padding="10dip"
                android:text="&lt;" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:onClick="setAsWallpaper"
                android:padding="10dip"
                android:text="Set as Background" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="25"
                android:onClick="gotoNextImage"
                android:padding="10dip"
                android:text=">" />
        </LinearLayout>

        <ImageView
            android:id="@+id/backgroundPreview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="Preview of the wallpaper to choose"
            android:scaleType="fitXY" />


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

【讨论】:

    【解决方案2】:

    试试这样的:使用框架布局

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <LinearLayout android:layout_gravity="top"
        android:gravity="top"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="25"
                android:onClick="gotoPreviousImage"
                android:padding="10dip"
                android:text="&lt;" />
    
            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:onClick="setAsWallpaper"
                android:padding="10dip"
                android:text="Set as Background" />
    
            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="25"
                android:onClick="gotoNextImage"
                android:padding="10dip"
                android:text=">" />
        </LinearLayout>
    
        <ImageView
            android:id="@+id/backgroundPreview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="Preview of the wallpaper to choose"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" />
    
        <com.google.android.gms.ads.AdView
            android:id="@+id/AdView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             android:layout_gravity="bottom"
            android:gravity="bottom"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="Unit_ID" />
    
    </FrameLayout>
    

    让我知道它是否有效

    【讨论】:

    • 谢谢,但三个按钮仍然是不可见的,或者它们在 Imageview 后面,与 RelativeLayout 相同:(
    • 在我的情况下看起来不错。我编辑了答案以将重力放在第一个线性布局上。试试这个
    • 大声笑。还是一样,还有其他想法吗? :D
    • 显示它现在的样子。因为在我的情况下,它看起来很好,而且代码也
    • 抱歉回复晚了,这是图片i.imgur.com/mhfFUuy.png我想我们需要缩放图片吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    相关资源
    最近更新 更多