0

In the following code , Why I can not set a custom size for my relativeLayout object? The resolution of my device is 240 * 320.

RelativeLayout relativeLayout;
    @Override
    protected void onCreate(Bundle bundle){
            super.onCreate(bundle);
    relativeLayout = new RelativeLayout(this);
    relativeLayout.getLayoutParams().width=240;
        relativeLayout.getLayoutParams().height=320;
    setContentView(relativeLayout);}
and
  • 341
  • 1
  • 3
  • 11

1 Answers1

1

You should try to use a layout parameter like this:

RelativeLayout relativeLayout = new RelativeLayout(this);
//You can put the background in another color to check were is the layout
//relativeLayout.setBackgroundResource(android.R.color.black);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(240, 320);
relativeLayout.setLayoutParams(lp);

setContentView(relativeLayout);
xiaomi
  • 6,553
  • 3
  • 29
  • 34