如何在相对布局中添加 admob

发布于 2025-01-02 00:16:02 字数 2154 浏览 3 评论 0原文

我开发了一个android应用程序。我的应用程序将在横向模式下使用。现在我想在左侧垂直放置一个广告。是否可以?如果不是那么请问如何将其水平放置在顶部?

我的布局是

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

    <ImageView android:id="@+id/s1" android:layout_width="wrap_content"
        android:src="@drawable/a_1" android:layout_height="wrap_content"
        android:padding="2dp" />

    <ImageView android:id="@+id/s2" android:layout_width="wrap_content"
        android:src="@drawable/a_2" android:layout_height="wrap_content"
        android:padding="2dp" />

    <ImageView android:id="@+id/s3" android:layout_width="wrap_content"
        android:src="@drawable/a_3" android:layout_height="wrap_content"
        android:padding="2dp" />

    <ImageView android:id="@+id/s4" android:layout_width="wrap_content"
        android:src="@drawable/a_4" android:layout_height="wrap_content"
        android:padding="2dp" />

    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_alignParentBottom="true"
        android:gravity="center" android:background="#00000000">

    <TextView android:id="@+id/l" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:paddingLeft="20dp"
        android:paddingRight="20dp" android:background="@drawable/letterbackground"
        android:textColor="#FFFFFFFF" android:textSize="60sp"
        android:shadowColor="#AA42d68b" android:shadowRadius="1.5"
        android:shadowDx="5" android:shadowDy="5" android:text="A" />
    </RelativeLayout>

    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:id="@+id/menu_widget" />
    </RelativeLayout>
</RelativeLayout>

I have developed an android application. My app will be used in landscape mode. Now I wanna place an ad at the left side vertically. Is it possible? if not then please how to place it horizontally at the top?

My layout is

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

    <ImageView android:id="@+id/s1" android:layout_width="wrap_content"
        android:src="@drawable/a_1" android:layout_height="wrap_content"
        android:padding="2dp" />

    <ImageView android:id="@+id/s2" android:layout_width="wrap_content"
        android:src="@drawable/a_2" android:layout_height="wrap_content"
        android:padding="2dp" />

    <ImageView android:id="@+id/s3" android:layout_width="wrap_content"
        android:src="@drawable/a_3" android:layout_height="wrap_content"
        android:padding="2dp" />

    <ImageView android:id="@+id/s4" android:layout_width="wrap_content"
        android:src="@drawable/a_4" android:layout_height="wrap_content"
        android:padding="2dp" />

    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_alignParentBottom="true"
        android:gravity="center" android:background="#00000000">

    <TextView android:id="@+id/l" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:paddingLeft="20dp"
        android:paddingRight="20dp" android:background="@drawable/letterbackground"
        android:textColor="#FFFFFFFF" android:textSize="60sp"
        android:shadowColor="#AA42d68b" android:shadowRadius="1.5"
        android:shadowDx="5" android:shadowDy="5" android:text="A" />
    </RelativeLayout>

    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:id="@+id/menu_widget" />
    </RelativeLayout>
</RelativeLayout>

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

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

发布评论

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

评论(2

对你再特殊 2025-01-09 00:16:02

您需要将 admob AdView 放入您的代码中(在您的活动或片段中)。当您收到实际广告时,该广告就会显示(水平位于顶部)。

这是一个应该有效的示例:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout 
        xmlns:myapp="http://schemas.android.com/apk/res/your.app.package"
        android:id="@+id/ad_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    </LinearLayout>

    <Put the rest of the layout here>
    </Put the rest of the layout here>
</RelativeLayout>

You need to put the admob AdView in your code (in your activity or fragment). The ad will show up when you receive an actual ad (horizontally at the top).

Here is a sample that should work:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout 
        xmlns:myapp="http://schemas.android.com/apk/res/your.app.package"
        android:id="@+id/ad_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    </LinearLayout>

    <Put the rest of the layout here>
    </Put the rest of the layout here>
</RelativeLayout>
梦幻的味道 2025-01-09 00:16:02

从 Admob 版本 6.4.1 开始,您甚至不必实例化 Activity,您可以直接将广告添加为视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<com.google.ads.AdView android:id="@+id/ad"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_centerHorizontal="true"
                       android:layout_alignParentBottom="true"
                       ads:adSize="BANNER"
                       ads:adUnitId="a151b78112c1df4"
                       ads:testDevices="TEST_EMULATOR,TEST_DEVICE_ID_GOES_HERE"
                       ads:loadAdOnCreate="true"/>
</RelativeLayout>

As of Admob version 6.4.1, you don't even have to instantiate the activity, you can add the ad as a view directly:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<com.google.ads.AdView android:id="@+id/ad"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_centerHorizontal="true"
                       android:layout_alignParentBottom="true"
                       ads:adSize="BANNER"
                       ads:adUnitId="a151b78112c1df4"
                       ads:testDevices="TEST_EMULATOR,TEST_DEVICE_ID_GOES_HERE"
                       ads:loadAdOnCreate="true"/>
</RelativeLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文