2015-09-04 46 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); 
相關問題