Set View Width Programmatically

后端 未结 7 2009
甜味超标
甜味超标 2020-12-01 09:57

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 t

相关标签:
7条回答
  • 2020-12-01 10:57

    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);
    
    0 讨论(0)
提交回复
热议问题