2012-02-10 49 views
14

嘿我想管理編輯的EditText框的高度和寬度在Android我試過edittext.setWidth(32);edittext.setEms(50);兩者都不工作請參閱我的下面的編碼coze我使用它來創建動態在的EditText的Android如何在android中編程設置EditText框的高度和寬度

private EditText createEditText() 
    { 
     final LayoutParams lparams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
     final EditText edittext = new EditText(this); 
     edittext.setLayoutParams(lparams); 
     edittext.setWidth(32); 
     edittext.setEms(50); 
     return edittext; 
    } 

回答

21
private EditText createEditText() 
{ 
    final LayoutParams lparams = new LayoutParams(50,30); // Width , height 
    final EditText edittext = new EditText(this); 
    edittext.setLayoutParams(lparams); 
    return edittext; 
} 

試試這個。

+5

哪'LayoutParams'導入? – Mann 2016-03-03 10:00:29

+2

需要進口ViewGroup.LayoutParams – MRK 2016-07-20 06:52:18

10
edittext.getLayoutParams().width=32; 
+0

感謝其工作正常感謝ü這麼多 – 2012-02-10 08:43:55

+6

這使我NullPointerExceptions的... :-( – JerabekJakub 2014-08-23 08:53:04

6
DisplayMetrics metrics = new DisplayMetrics(); 
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); 
wm.getDefaultDisplay().getMetrics(metrics); 
final float height = metrics.heightPixels; 

EditText edittext = new EditText(this); 

edittext.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,(int) (height/2))); 
-2

試試下面的代碼: -

Display display = getWindowManager().getDefaultDisplay(); 
    int screenWidth = display.getWidth(); 
    int screenHeight = display.getHeight(); 

profile_pic.getLayoutParams().height=(screenHeight/6); 
    profile_pic.getLayoutParams().width=(screenHeight/6); 
1
RelativeLayout.LayoutParams.width = 32; 
RelativeLayout.LayoutParams.height = 50; 

爲我工作。例如

lparams.width = 32; 
0
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) edittext.getLayoutParams(); 
      params.width = mScreenWidth/5; 
      params.height = mScreenWidth/5; 
      edittext.setLayoutParams(params); 
相關問題