gpt4 book ai didi

android - 如何在设备屏幕右侧显示 AdMob 广告

转载 作者:行者123 更新时间:2023-11-30 02:25:04 30 4
gpt4 key购买 nike

我正在尝试将 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;
}

有人可以帮助我吗?谢谢

最佳答案

好吧,我想我知道问题出在哪里了。

Activity 的外部布局始终是 FrameLayout。因为您要将 RelativLayout 添加到 Activity 布局,父级是 FrameLayout 而不是 RelativeLayout 这就是为什么会出现类转换异常的原因。需要 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;
}

关于android - 如何在设备屏幕右侧显示 AdMob 广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27986384/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com