2012-02-10 16 views
14

hej Chcę zarządzać wysokością i szerokością EditText Box programowo w Androidzie próbowałem edittext.setWidth(32); i edittext.setEms(50); oba nie działają Proszę zobaczyć mój poniższy kodowanie coze Używam go do tworzenia dynamicznych EditText w androidjak ustawić programowo wysokość i szerokość EditText w Androidzie

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; 
    } 

Odpowiedz

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

Spróbuj tego.

+5

Które 'LayoutParams' importować? – Mann

+2

trzeba importować ViewGroup.LayoutParams – MRK

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

podziękować jej działa prawidłowo thank u tyle –

+6

To sprawia mi NullPointerExceptions ... :-( – JerabekJakub

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

Spróbuj następujący kod: -

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; 

pracuje dla mnie. na przykład

lparams.width = 32; 
0
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) edittext.getLayoutParams(); 
      params.width = mScreenWidth/5; 
      params.height = mScreenWidth/5; 
      edittext.setLayoutParams(params); 
Powiązane problemy