我试图设置的宽度和高度的代码,以显示一个免费应用程序的广告,我的工作。除了这个广告之外,所有的用户界面都是用 XML 完成的。下面是显示广告的代码。这是在 onCreate
。
AdView adView = new AdView(this,"ad_url","my_ad_key",true,true);
LinearLayout layout = (LinearLayout) findViewById(R.id.testing);
adView.setAdListener(this);
layout.addView(adView);
这会导致广告像这样显示。正如你所看到的宽度并没有完全填满整个页面。
现在,当我将代码更改为这个来尝试设置大小:
AdView adView = new AdView(this,"ad_url","my_ad_key",true,true);
LinearLayout layout = (LinearLayout) findViewById(R.id.testing);
adView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
adView.setAdListener(this);
layout.addView(adView);
我明白了:
我已经尝试了所有不同的 FILL_PARENT
和 WRAP_CONTENT
的组合都有相同的结果。我是不是遗漏了什么?或者另一种方式,我可以有广告填补宽度的设备,而不填补高度?非常感谢您的建议和帮助。
当我试图把它放到 XML 中时,我得到了一个例外:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/main_layout">
<fragment
android:name="com.jasoncrosby.app.tipcalculatorfree.MainButtonFragment"
android:id="@+id/button_frag"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<com.adsdk.sdk.banner.AdView
android:id="@+id/banner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
> 09-09 22:39:43.448: E/AndroidRuntime(586): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jasoncrosby.app.tipcalculatorfree/com.jasoncrosby.app.tipcalculatorfree.MainActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class com.adsdk.sdk.banner.AdView
09-09 22:39:43.448: E/AndroidRuntime(586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.os.Looper.loop(Looper.java:132)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.app.ActivityThread.main(ActivityThread.java:4025)
09-09 22:39:43.448: E/AndroidRuntime(586): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 22:39:43.448: E/AndroidRuntime(586): at java.lang.reflect.Method.invoke(Method.java:491)
09-09 22:39:43.448: E/AndroidRuntime(586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-09 22:39:43.448: E/AndroidRuntime(586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-09 22:39:43.448: E/AndroidRuntime(586): at dalvik.system.NativeStart.main(Native Method)
09-09 22:39:43.448: E/AndroidRuntime(586): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class com.adsdk.sdk.banner.AdView
09-09 22:39:43.448: E/AndroidRuntime(586): at android.view.LayoutInflater.createView(LayoutInflater.java:579)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:671)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
09-09 22:39:43.448: E/AndroidRuntime(586): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.app.Activity.setContentView(Activity.java:1780)
09-09 22:39:43.448: E/AndroidRuntime(586): at com.jasoncrosby.app.tipcalculatorfree.MainActivity.onCreate(MainActivity.java:95)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
09-09 22:39:43.448: E/AndroidRuntime(586): ... 11 more
09-09 22:39:43.448: E/AndroidRuntime(586): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
09-09 22:39:43.448: E/AndroidRuntime(586): at java.lang.ClassMembers.getConstructorOrMethod(ClassMembers.java:235)
09-09 22:39:43.448: E/AndroidRuntime(586): at java.lang.Class.getConstructor(Class.java:459)
09-09 22:39:43.448: E/AndroidRuntime(586): at android.view.LayoutInflater.createView(LayoutInflater.java:551)
09-09 22:39:43.448: E/AndroidRuntime(586): ... 21 more