将 Admob 添加到 ScrollView ->广告横幅消失

发布于 2024-11-07 11:42:01 字数 2075 浏览 2 评论 0原文

嘿伙计们 到目前为止,我成功地将 Admob 实现为正常的线性布局。 现在我添加了一个额外的 scrollview 并且 adbanner 消失了。我不知道我能做什么来对抗它。

遵循我添加了 scrollview 的 .xml 中的代码:

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/linearLayout"
   android:orientation="vertical"
   android:layout_width="fill_parent" android:layout_height="match_parent">
   <ScrollView android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_width="match_parent">
     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent"   android:layout_height="match_parent" android:orientation="vertical">

     [whole bunch of layout elements whoch shouldn´t affect the adbanner]

     </LinearLayout>
   </ScrollView>    

在我的线性布局中,adbanner 仍然有效,整个 adbanner 位置是在主 activitiy.java 文件中完成的(在 taiic.com 教程的帮助下完成此操作)

    // Lookup R.layout.main
    LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout); 

    // Create the adView
    // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
    String pubID = "xxxxxxxxxxxxxxxxxx";
    AdView adView = new AdView(this, AdSize.BANNER, pubID);

    // Add the adView to it
    layout.addView(adView);

    // Initiate a generic request to load it with an ad
    AdRequest request = new AdRequest();
    request.setTesting(true);

    adView.loadAd(request);

任何人都可以告诉我要更改什么或添加什么代码在 scrollview 中实现 admob 横幅?

编辑:

添加

<com.admob.android.ads.AdView
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:backgroundColor="#000000"
myapprimaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
android:alignParentBottom="true"/>

我尝试在 .xml 中的最后两行之间

  </LinearLayout>
     [here]
  </ScrollView>

,但随后收到错误“错误:解析 XML 时出错:未绑定前缀”

欢呼

Hey folks
I managed to implement Admob into a normal linear layout so far.
Now I added a additional scrollview and the adbanner disappeared. I don´t know what I can do against it.

Follows the code from the .xml where I added the scrollview:

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/linearLayout"
   android:orientation="vertical"
   android:layout_width="fill_parent" android:layout_height="match_parent">
   <ScrollView android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_width="match_parent">
     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent"   android:layout_height="match_parent" android:orientation="vertical">

     [whole bunch of layout elements whoch shouldn´t affect the adbanner]

     </LinearLayout>
   </ScrollView>    

In my linear layout ,where the adbanner still works, the whole adbanner position was done in the main activitiy.java file (did this with help of the tutorial at taiic.com)

    // Lookup R.layout.main
    LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout); 

    // Create the adView
    // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
    String pubID = "xxxxxxxxxxxxxxxxxx";
    AdView adView = new AdView(this, AdSize.BANNER, pubID);

    // Add the adView to it
    layout.addView(adView);

    // Initiate a generic request to load it with an ad
    AdRequest request = new AdRequest();
    request.setTesting(true);

    adView.loadAd(request);

Can anybody tell me what to change or what code to add when implementing an admob banner into a scrollview?

edit:

i tried to add

<com.admob.android.ads.AdView
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:backgroundColor="#000000"
myapprimaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
android:alignParentBottom="true"/>

between last two lines in the .xml

  </LinearLayout>
     [here]
  </ScrollView>

but then im getting the error "error: Error parsing XML: unbound prefix"

cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携君以终年 2024-11-14 11:42:01

关于解析错误:

这是问题中的拼写错误吗? myapprimaryTextColor="#FFFFFF" 而不是 myapp:primaryTextColor="#FFFFFF" 。这会给你带来 xml 解析错误。


关于布局:

使用RelativeLayout。 工作代码位于帖子末尾。首先,一些理论:)

您的 ScrollView 占据了整个屏幕,这就是您看不到 admob 视图的原因。定义滚动视图后,所有屏幕都对其可用,因此它会占用它。 admob 视图实际上绘制在屏幕下方。它可以在这个例子中重现:

非工作布局

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/linearLayout"
   android:orientation="vertical"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent">
   <ScrollView android:id="@+id/scrollView1" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent">
         <LinearLayout android:id="@+id/linearLayout1" 
         android:layout_width="fill_parent"   
         android:layout_height="fill_parent" 
         android:orientation="vertical"
         >
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test1"
            />
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test2"
            />
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test3"
            />
         </LinearLayout>
   </ScrollView>    
        <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Test4"
        />
   </LinearLayout>

如果您使用RelativeLayout,您可以对其进行设置,以便admob与屏幕底部对齐,滚动视图在其上方,剩余的可用空间。

工作布局

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent">
        <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Test4"
        android:id="@+id/test4"
        android:layout_alignParentBottom="true"
        />
       <ScrollView android:id="@+id/scrollView1" 
           android:layout_height="wrap_content" 
           android:layout_width="fill_parent"
           android:layout_above="@id/test4"
           >
         <LinearLayout android:id="@+id/linearLayout1" 
         android:layout_width="fill_parent"   
         android:layout_height="fill_parent" 
         android:orientation="vertical"
         >
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test1"
            />
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test2"
            />
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test3"
            />
         </LinearLayout>
       </ScrollView>    
   </RelativeLayout>

About the parsing error:

Is this a typo in the question? myapprimaryTextColor="#FFFFFF" instead of myapp:primaryTextColor="#FFFFFF" . This would give you the xml parse error.


About the layouts:

Use a RelativeLayout. The working code is at the end of the post. First, some theory :)

Your ScrollView is taking the whole screen, that's why you don't see the admob view. When the scrollview is defined, all the screen is availabel to it, so it takes it. The admob view is actually drawn below your screen. It can be reproduced in this example:

non-working layout

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/linearLayout"
   android:orientation="vertical"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent">
   <ScrollView android:id="@+id/scrollView1" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent">
         <LinearLayout android:id="@+id/linearLayout1" 
         android:layout_width="fill_parent"   
         android:layout_height="fill_parent" 
         android:orientation="vertical"
         >
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test1"
            />
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test2"
            />
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test3"
            />
         </LinearLayout>
   </ScrollView>    
        <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Test4"
        />
   </LinearLayout>

If you use a RelativeLayout instead, you can set it up so the admob is aligned to the bottom of the screen, and the scrollview above it, taking the rest of the available space.

working layout

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent">
        <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Test4"
        android:id="@+id/test4"
        android:layout_alignParentBottom="true"
        />
       <ScrollView android:id="@+id/scrollView1" 
           android:layout_height="wrap_content" 
           android:layout_width="fill_parent"
           android:layout_above="@id/test4"
           >
         <LinearLayout android:id="@+id/linearLayout1" 
         android:layout_width="fill_parent"   
         android:layout_height="fill_parent" 
         android:orientation="vertical"
         >
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test1"
            />
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test2"
            />
            <EditText  
            android:layout_width="fill_parent" 
            android:layout_height="300dp" 
            android:text="Test3"
            />
         </LinearLayout>
       </ScrollView>    
   </RelativeLayout>
萧瑟寒风 2024-11-14 11:42:01

我使用了 admob xml 版本,这就是我所使用的并且它有效。广告位于顶部。只需复制并粘贴,您很快就会滚动。

<?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/com.yourproject.here"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <com.google.ads.AdView android:id="@+id/adView"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           ads:adUnitId="a14dc1c9d6xxxxx"
                           ads:adSize="BANNER" />

    <ScrollView android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:fillViewport="true" > 

        [whole bunch of layout elements whoch shouldn´t affect the adbanner]

    </ScrollView>

</LinearLayout>

I used the admob xml version and that is what I use and it works. The ad is at the top. Just copy and paste and you will be scrolling along shortly.

<?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/com.yourproject.here"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <com.google.ads.AdView android:id="@+id/adView"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           ads:adUnitId="a14dc1c9d6xxxxx"
                           ads:adSize="BANNER" />

    <ScrollView android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:fillViewport="true" > 

        [whole bunch of layout elements whoch shouldn´t affect the adbanner]

    </ScrollView>

</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文