布局参数:我使用什么导入?

Layout Params: What imports do I use?

我和这个人的情况相似: Implementing Admob banner when setContentView() is used for the Surfaceview

我的 "sGame" 创建了一个横向视图游戏,我想要在顶部放置一个 admob 广告。

我认为答案可行,但我不确定要为 "layout.setLayoutParams".

导入哪个 LayoutParams

当我在 LayoutParams 上按 Alt+Enter 时,我得到以下选项:

import android.view.ViewGroup.LayoutParams;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.FrameLayout.LayoutParams;
import android.widget.GalleryLayout.LayoutParams;
import android.support.v7.widget.LinearLayoutCompat.LayoutParams;
//... quite a few more options

这取决于 layout 的类型。

来自 link,它是 RelativeLayout,因此您需要导入 android.widget.RelativeLayout.LayoutParams

不同的布局管理器使用不同的布局参数。很明显:RelativeLayout 使用 RelativeLayout.LayoutParamsLinearLayout 使用 LinearLayout.LayoutParams

也就是说,你需要使用 RelativeLayout.LayoutParams:

import android.widget.RelativeLayout.LayoutParams;

因为在你提到的link中,答案告诉你使用RelativeLayout

现在,让我给你一些额外的信息。如果你使用错误的参数怎么办? InvalidCastException 被抛出。当您遇到该异常时,您可能会知道哪里出了问题。您使用了错误类型的参数。 Android 正在尝试将该类型的参数转换为正确的类型!