2011年12月16日金曜日

AdMobを中央に配置する。

AdMobを中央に配置する。

副題:その空間に、愛想笑いを。

何も考慮せずにAdMobのlayoutを作成すると、AdMobの広告は物理画面の左詰めになる。この場合、端末が横位置になった場合、広告の右側に無意味な空白が発生する。
if(あなたの考え=="別に構わないじゃないか") return;

このような無意味な空白はAdMob側で何か措置を施して頂きたいが、現状ではどうにもならないので、プログラマ側でなんとかする。
無意味な空間を、なんとか取りツクロうのである。言わば、気まずい雰囲気の中で行われる、愛想笑いのような仕掛けだ。
if(あなたの表情=="笑い顔には自信が無い") return;

取り敢えず考えられるのは、広告を横方向の中央に配置すれば良い、ということであろう。中央に配置することにより、無意味な空間を左右に分散させるのである。これにより、無意味と感じる程度が低下することが期待できる。
if(あなたの感想=="左側にも無意味な空間が広がるだけだ") return;

背景が画像である場合は、FrameLayoutを使うことになるであろう。そのためのlayout用xmlのsample codeは次のとおりである。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ViewGroup"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#a00"
    >
    ここに背景用のViewを設置する。
    <LinearLayout
        android:id="@+id/AdView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        >
    </LinearLayout>
</FrameLayout>

他のViewがBottunやScrollView等であれば、次のようにRelativeLayoutを用いることになる。
<?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"
    android:background="#a00"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        >
        <LinearLayout
            android:id="@+id/AdView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
        </LinearLayout>
    </LinearLayout>
    ここに他のViewを書いていく。
</RelativeLayout>

上記例では、中央に配置したことを視覚的に明確にするため、背景を赤色で塗ってある。しかし、広告を目立たせるためには、背景を目立たない色にするべきであろう。
if(あなたの趣味=="背景は広告よりも目立つようにすべきだ") return;

javaのcodeは、AdMob Version 4.3.1の実装に書いたとおりである。

後日談は、画面サイズに応じてAdMobのサイズを変えるに書きました。

0 件のコメント:

コメントを投稿