1. 程式人生 > >Android中在程式碼中設定控制元件的寬和高

Android中在程式碼中設定控制元件的寬和高

//在程式碼中設定控制元件大小的方法
private Button mbtn;
mbtn = (Button) findViewById(R.id.btn_test);
LayoutParams lp;
lp=mbtn.getLayoutParams();
lp.width=100;
lp.height=200;
mbtn.setLayoutParams(lp);

//在程式碼中設定介面大小的方法:

Display display = getWindowManager().getDefaultDisplay(); // 為獲取螢幕寬、高
Window window = getWindow();
LayoutParams windowLayoutParams = window.getAttributes(); // 獲取對話方塊當前的引數值
windowLayoutParams.width = (int) (display.getWidth() * 0.7); // 寬度設定為螢幕的0.95
windowLayoutParams.height = (int) (display.getHeight() * 0.1); // 高度設定為螢幕的0.6
windowLayoutParams.alpha = 0.5f;// 設定透明度