當前位置:編程學習大全網 - 網站源碼 - Android中如何在代碼中設置View的寬和高?

Android中如何在代碼中設置View的寬和高?

//在代碼中設置控件大小的方法

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;// 設置透明度

  • 上一篇:Abtest源代碼
  • 下一篇:富豪麻將裏的捕魚和捕魚大作戰壹樣嗎
  • copyright 2024編程學習大全網