以编程方式设置视图宽度视图、宽度、方式

2023-09-12 09:27:19 作者:樱桃味的初夏

我想设置code视图的宽度和高度,以显示广告的免费应用程序我的工作。所有UI的是除此广告做在XML中。这里是code所显示的广告。它是在的onCreate

  AD浏览报AD浏览报=新的AD浏览报(这一点,ad_url,my_ad_key,真正的,真实的);
的LinearLayout布局=(的LinearLayout)findViewById(R.id.testing);
adView.setAdListener(本);
layout.addView(AD浏览报);
 

这导致广告被显示成这样。正如你所看到的宽度并不完全填满整个页面。

现在,当我改变code这个尝试设置大小:

  AD浏览报AD浏览报=新的AD浏览报(这一点,ad_url,my_ad_key,真正的,真实的);
的LinearLayout布局=(的LinearLayout)findViewById(R.id.testing);
adView.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                       LayoutParams.WRAP_CONTENT));
adView.setAdListener(本);
layout.addView(AD浏览报);
 
word2010中怎么改变视图的宽度

我得到这样的:

我已经试过 FILL_PARENT WRAP_CONTENT 所有人的所有不同的组合具有相同的结果。有我丢失的东西?或者另一种方式,我可以有广告占满设备的宽度没有填写的高度?任何建议和帮助是很大的AP preciated。

修改的,当我试图把它放在XML我得​​到一个异常:

 <的LinearLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
机器人:ID =@ + ID / main_layout>

<片段
    机器人:名称=com.jasoncrosby.app.tipcalculatorfree.MainButtonFragment
机器人:ID =@ + ID / button_frag
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT/>
< com.adsdk.sdk.banner.AdView
    机器人:ID =@ + ID /旗帜
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT/>

< / LinearLayout中>
 

 > 09-09 22:39:43.448:E / AndroidRuntime(586):java.lang.RuntimeException的:无法启动的活动ComponentInfo{com.jasoncrosby.app.tipcalculatorfree/com.jasoncrosby.app.tipcalculatorfree.MainActivity}: android.view.InflateException:二进制XML文件中的行#14:错误充气类com.adsdk.sdk.banner.AdView
09-09 22:39:43.448:E / AndroidRuntime(586):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.app.ActivityThread.access $ 1500(ActivityThread.java:122)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1002)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.os.Handler.dispatchMessage(Handler.java:99)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.os.Looper.loop(Looper.java:132)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.app.ActivityThread.main(ActivityThread.java:4025)
09-09 22:39:43.448:E / AndroidRuntime(586):在java.lang.reflect.Method.invokeNative(本机方法)
09-09 22:39:43.448:E / AndroidRuntime(586):在java.lang.reflect.Method.invoke(Method.java:491)
09-09 22:39:43.448:E / AndroidRuntime(586):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
09-09 22:39:43.448:E / AndroidRuntime(586):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-09 22:39:43.448:E / AndroidRuntime(586):在dalvik.system.NativeStart.main(本机方法)
09-09 22:39:43.448:E / AndroidRuntime(586):android.view.InflateException:二进制XML文件中的行#14:错误充气类com.adsdk.sdk.banner.AdView所致
09-09 22:39:43.448:E / AndroidRuntime(586):在android.view.LayoutInflater.createView(LayoutInflater.java:579)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:671)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.view.LayoutInflater.inflate(LayoutInflater.java:479)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.view.LayoutInflater.inflate(LayoutInflater.java:391)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.view.LayoutInflater.inflate(LayoutInflater.java:347)
09-09 22:39:43.448:E / AndroidRuntime(586):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.app.Activity.setContentView(Activity.java:1780)
09-09 22:39:43.448:E / AndroidRuntime(586):在com.jasoncrosby.app.tipcalculatorfree.MainActivity.onCreate(MainActivity.java:95)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
09-09 22:39:43.448:E / AndroidRuntime(586):11 ...更多
09-09 22:39:43.448:E / AndroidRuntime(586):java.lang.NoSuchMethodException:产生的原因< INIT> [类android.content.Context,接口android.util.AttributeSet]
09-09 22:39:43.448:E / AndroidRuntime(586):在java.lang.ClassMembers.getConstructorOrMethod(ClassMembers.java:235)
09-09 22:39:43.448:E / AndroidRuntime(586):在java.lang.Class.getConstructor(Class.java:459)
09-09 22:39:43.448:E / AndroidRuntime(586):在android.view.LayoutInflater.createView(LayoutInflater.java:551)
09-09 22:39:43.448:E / AndroidRuntime(586):21 ...更多
 

解决方案

这code,让你充满旗帜,以最大宽度和保持率。 这将只能在画像。当你旋转设备,你必须重新创建广告。 在景观,你应该见好就收广告原样,因为这将是相当大的模糊。

 显示显示= getWindowManager()getDefaultDisplay()。
INT宽度= display.getWidth();
双率=((浮点)(宽))/ 300.0;
INT高度=(int)的(比* 50);

AD浏览报AD浏览报=新的AD浏览报(这一点,ad_url,my_ad_key,真正的,真实的);
的LinearLayout布局=(的LinearLayout)findViewById(R.id.testing);
mAdView.setLayoutParams(新FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,高度));
adView.setAdListener(本);
layout.addView(AD浏览报);
 

I am trying to set the width and height of a view in code to show an ad for a free app I am working on. All of the UI is done in XML with the exception of this ad. Here is the code that displays the ad. It is in 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);

This results in the ad being displayed like this. As you can see the width doesn't quite fill the whole page.

Now when I change the code to this to attempt to set the size:

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);

I get this:

I have tried all different combinations of FILL_PARENT and WRAP_CONTENT all with the same result. Is there something I am missing? Or another way I can have the ad fill the width of the device without filling the height? Any suggestions and help is greatly appreciated.

EDIT when I try to put it in XML I get an exception:

<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

解决方案

This code let you fill the banner to the maximum width and keep the ratio. This will only work in portrait. You must recreate the ad when you rotate the device. In landscape you should just leave the ad as is because it will be quite big an blurred.

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);