[英]Add 7 buttons dynamically to adjust all the screen sizes to it's width and height in Android
[英]How to adjust the same height for all the buttons dynamically?
我正在生成自己的Button
視圖(非從Android Button
類擴展)。
按鈕的高度取決於電話屏幕的尺寸。 這個應用程式在所有手機上的所有螢幕按鈕應顯示相同的高度,並可能screen
所有screen
尺寸。
然后,在將按鈕添加到layout
之前,我應該知道按鈕的高度大小,但是在將其繪制在屏幕上之前,我不知道它的高度。
如何根據屏幕尺寸dynamically
在所有按鈕上設置相同的高度?
謝謝
嘗試這個。 您可以考慮使用尺寸為320 x480的電話,並檢查一下單個按鈕的最佳寬度和高度。下面給出的類根據設備的寬度和高度計算寬度和高度。 假設您知道按鈕的寬度/高度為40,然后將40作為參數傳遞給下面給出的類的calculateWidth()/ calculateHeight()函數,則在運行時寬度和高度將動態設置。
public class DimensionManager {
private int _screenWidth;
private int _screenHeight;
private int _baseScreenWidth = 320;
private int _baseScreenHeight = 480;
public DimensionManager(Activity act) {
WindowManager w = act.getWindowManager();
Display d = w.getDefaultDisplay();
_screenWidth = d.getWidth();
_screenHeight = d.getHeight();
}
/**
*
* @param baseViewWidth Width considering a base Layout.
* @return Returns corresponding width for the device and orientation.
*/
public int calculateWidth(int baseViewWidth) {
return (baseViewWidth * _screenWidth) / _baseScreenWidth;
}
/**
*
* @param baseViewHeight Height considering a base Layout.
* @return Returns corresponding height for the device and orientation.
*/
public int calculateHeight(int baseViewHeight) {
return (baseViewHeight * _screenHeight) / _baseScreenHeight;
}
}
最后我用onLayout和layout調用了我的方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.