如何在设备上显示与屏幕右侧对齐的AdMob广告

时间:2015-01-16 14:42:03

标签: android android-layout android-relativelayout

我正在尝试将Admob横幅对齐到设备屏幕的右侧。但它对我不起作用。我正在创建这样的横幅

  RelativeLayout layout = new RelativeLayout(activity);
                activity.addContentView(layout, new RelativeLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT));
                setLayout(layout, llType);

此处调用setLayout,根据类型

设置横幅的布局
   private void setLayout(RelativeLayout layout, int type) {
    switch (type) {
        case AdLayoutType.Top:
            layout.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
            break;
        case AdLayoutType.Bottom:
            layout.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
            break;
        case AdLayoutType.Left:
            RelativeLayout.LayoutParams leftParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layout.setLayoutParams(leftParams);
             break;
        case AdLayoutType.Right:
//This does not work as it gives Class cast exception
                RelativeLayout.LayoutParams rightParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                layout.setLayoutParams(rightParams);
             break;
            default:
                break;
        }

有人可以帮助我吗? 谢谢     }

1 个答案:

答案 0 :(得分:1)

我想我知道这个问题是什么。

您的Activity的外部布局始终是FrameLayout。因为你将RelativLayout添加到Activity布局中,父是FrameLayout而不是RelativeLayout这就是为什么有一个类强制转换Exception。预计FrameLayout.LayoutParams。

这里是xml:

中的示例
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="320dp"
    android:layout_height="50dp"
    android:background="@color/white"
    android:layout_gravity="top|center_horizontal">
    </RelativeLayout>

<RelativeLayout
    android:layout_width="320dp"
    android:layout_height="50dp"
    android:background="@color/white"
    android:layout_gravity="bottom|center_horizontal">
</RelativeLayout>

<RelativeLayout
    android:layout_width="320dp"
    android:layout_height="50dp"
    android:background="@color/white"
    android:layout_alignParentLeft="true" <!-- has no effect, default align is left -->
    android:layout_gravity="center_vertical">
</RelativeLayout>

<RelativeLayout
    android:layout_width="320dp"
    android:layout_height="50dp"
    android:background="@color/white"
    android:layout_alignParentRight="true" 
<!-- has no effect, cause parent is no RelativeLayout this is causing class cast exception -->
    android:layout_gravity="center_vertical">
</RelativeLayout>
</FrameLayout>

删除工作示例的评论。

如果使用alignParentBottom insted auf layout_gravity,则应该有相同的异常。在第二种情况下。

因此,要使代码工作,首先添加父RelativeLayout,或者仅使用FrameLayout.LayoutParams。

示例(不是testet):

    private void setLayout(RelativeLayout layout, int type) {
        switch (type) {
            case AdLayoutType.Top:
                layout.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                break;
            case AdLayoutType.Bottom:
                layout.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
                break;
            case AdLayoutType.Left:
                FrameLayout.LayoutParams leftParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                layout.setLayoutParams(leftParams);
                layout.setGravity(Gravity.LEFT);
                 break;
            case AdLayoutType.Right:
                    FrameLayout.LayoutParams rightParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
                    layout.setLayoutParams(rightParams);
                    layout.setGravity(Gravity.RIGHT);
                 break;
                default:
                    break;
            }