我和这个人的情况类似:当setContentView()用于Surfaceview时实现Admob标语
我的“ 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
不同的布局管理器使用不同的布局参数。这非常明显:RelativeLayout
使用RelativeLayout.LayoutParams
,LinearLayout
使用LinearLayout.LayoutParams
等。
这意味着,您需要使用 RelativeLayout.LayoutParams
:
import android.widget.RelativeLayout.LayoutParams;
因为在您提到的链接中,答案告诉您使用RelativeLayout
。
现在,让我给您一些额外的信息。如果您使用了错误的参数怎么办?一个InvalidCastException
被抛出。当您遇到该异常时,您可能会知道哪里出了问题。您使用了错误的Params类型。Android正在尝试将这种类型的Params强制转换为正确的类型!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句