2015-09-04 95 views
0

在下面的代码中,为什么我无法为我的relativeLayout对象设置自定义尺寸?我的设备的分辨率为240 * 320。为什么我无法为我的relativeLayout对象设置自定义尺寸

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);} 
+1

为什么不使用MATCH_PARENT? –

+0

我可以使用'match_parent'。但我想要这个问题的科学原因。 – and

+1

http://stackoverflow.com/questions/3224193/set-the-layout-weight-of-a-textview-programmatically并确保你的屏幕尺寸实际上是240px(而不是dp) –

回答

1

你应该尽量使用这样的布局参数:

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); 
相关问题