我正在尝试在代码中设置视图的宽度和高度,以显示我正在处理的免费应用的广告。除了此广告之外,所有UI都以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
答案 0 :(得分:62)
您可以使用下面的代码,如果您只需要影响特定值,而不是触及他人:
view.getLayoutParams().width = newWidth;
答案 1 :(得分:59)
此代码可让您将横幅填充到最大宽度并保持比率。 这只适用于肖像。您必须在旋转设备时重新创建广告。 在风景中你应该保留广告,因为它会变得非常模糊。
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
double ratio = ((float) (width))/300.0;
int height = (int)(ratio*50);
AdView adView = new AdView(this,"ad_url","my_ad_key",true,true);
LinearLayout layout = (LinearLayout) findViewById(R.id.testing);
mAdView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,height));
adView.setAdListener(this);
layout.addView(adView);
答案 2 :(得分:6)
在代码中添加以下行:
spin27.setLayoutParams(new LinearLayout.LayoutParams(200, 120));
答案 3 :(得分:3)
<div class="segment ui">
<div class="ui top attached steps" [class.one]="steps.length == 1" [class.two]="steps.length == 2" [class.three]="steps.length == 3" [class.four]="steps.length == 4" [class.five]="steps.length == 5">
<div class="step" *ngFor="let item of steps">
<i class="industry icon"></i>
<div class="content">
<div class="title">test title</div>
<div class="description">test content</div>
</div>
</div>
</div>
<div class="ui segment" *ngFor="let item of steps">
<!--
@========================================
HERE IS A Quotation
I need to inject only one step component content that match the condition Stepper.activeStep == step.sid
Note : Please I do not want to use ng-template in app.component.html :)
@========================================
-->
</div>
</div>
答案 4 :(得分:0)
LayoutParams的第一个参数是宽度,第二个参数是高度。因此,如果您希望宽度为FILL_PARENT,但宽度为20px,则使用新的LayoutParams(FILL_PARENT,20)。当然,你不应该在你的代码中使用实际的像素;你需要将它转换为与密度无关的像素,但你明白了。此外,您需要确保您的父LinearLayout具有您正在寻找的正确宽度和高度。似乎是你希望LinearLayout在宽度方面填充父级,然后让adview填充那个linearlayout,所以你可能需要指定android:layout_width:“fill_parent”和android:layout_height:“wrap_content”in你的线性布局的xml。
答案 5 :(得分:0)
在mdpi设备中检查..如果广告正确显示,则错误应该是“px”到“dip”转换..
答案 6 :(得分:0)
hsThumbList.setLayoutParams(new LayoutParams(100, 400));