Android View align bottom (programmatically) without XML

后端 未结 4 1682
春和景丽
春和景丽 2021-01-01 17:52

I got a problem with setting aligning my view programatically to the bottom of the screen. Basically I just want to place my admob advert at the screen bottom. I can\'t do i

相关标签:
4条回答
  • 2021-01-01 18:23

    dragonfly's answer worked for me. Here is the complete code snippet for the reference

    adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxxxxx");
    panel = new MySurfaceView(this);
    
    AdRequest adRequest = new AdRequest();
    adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
    
    RelativeLayout rl = new RelativeLayout(this);
    RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    
    lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    
    rl.addView(panel);
    rl.addView(adView, lay);
    
    setContentView(rl);
    
    adView.loadAd(adRequest);
    
    0 讨论(0)
  • 2021-01-01 18:25

    Add the advert to the RelativeLayout with LayoutParams as follows:

    RelativeLayout.LayoutParams rparams = (LayoutParams) rl
                .getLayoutParams();
        rparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        ((ViewGroup) rl).addView(adView, rparams);
    

    instead of just

    rl.addView(adView);   
    

    Good luck! :)

    0 讨论(0)
  • 2021-01-01 18:32
    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
     p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
    
    0 讨论(0)
  • 2021-01-01 18:38
        ImageButton buttonAdvance = new ImageButton(this);
        RelativeLayout.LayoutParams params = new       RelativeLayout.LayoutParams(160, 160);
        params.leftMargin = 5;
        params.bottomMargin = 4;
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        buttonAdvance.setLayoutParams(params);
    
    0 讨论(0)
提交回复
热议问题