프로그래밍 방식으로보기 너비 설정
현재 작업중인 무료 앱에 대한 광고를 표시하기 위해 코드에서보기의 너비와 높이를 설정하려고합니다. 이 광고를 제외한 모든 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
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);
You can use something like code below, if you need to affect only specific value, and not touch others:
view.getLayoutParams().width = newWidth;
in code add below line:
spin27.setLayoutParams(new LinearLayout.LayoutParams(200, 120));
yourView.setLayoutParams(new LinearLayout.LayoutParams(width, height));
The first parameter to LayoutParams is the width and the second is the height. So if you want the width to be FILL_PARENT, but the width to be, say, 20px, then use something new LayoutParams(FILL_PARENT, 20). Of course you should never use actual pixels in your code; you'll need to conver that to density-independent pixels, but you get the idea. Also, you need to make sure your parent LinearLayout has the right width and height that you're looking for. Seems to be you want the LinearLayout to fill the parent width-wise and then have the adview fill that linearlayout witdh-wise as well, so you probably need to specify android:layout_width:"fill_parent" and android:layout_height:"wrap_content" in your linear layout's xml.
mdpi 기기에서 확인하세요. 광고가 올바르게 표시되면 오류가 'px'에서 'dip'로 변환되어야합니다 ..
hsThumbList.setLayoutParams(new LayoutParams(100, 400));
참고 URL : https://stackoverflow.com/questions/12343376/set-view-width-programmatically
'IT TIP' 카테고리의 다른 글
make : 규칙 호출 규칙 (0) | 2020.11.26 |
---|---|
템플릿을 다른 템플릿에 어떻게 삽입합니까? (0) | 2020.11.26 |
Mongoose JS를 통한 MongoDB-findByID 란 무엇입니까? (0) | 2020.11.26 |
Groovy에서 조건부 수집을 작성하는 방법은 무엇입니까? (0) | 2020.11.26 |
powershell을 사용하여 레지스트리 키의 값과 값만 가져 오는 방법 (0) | 2020.11.26 |