2014-04-04 38 views
1

我想根據屏幕大小更改視圖的高度。對於3英寸的屏幕來說,它必須是60dp,對於5英寸的屏幕則必須是100dp。我怎樣才能得到這個?根據Android中的屏幕分辨率更改視圖的高度

+0

您需要在demen中定義不同的尺寸並在視圖中使用它 –

+0

正如您所詢問的那樣,嘗試獲取屏幕高度,然後將高度除以某個值,這可能會與輸出值一起設置。我猜這是最簡單的解決方案。 – OAEI

回答

2

你可以得到設備的屏幕尺寸(高度和寬度),然後你可以設置元素的高度和寬度(Button/Edittext/LinearLayout),但記住它需要一個正確的計算來顯示一個元素在正確的位置。

你可以做這樣的事情:

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int screenWidth = size.x; // int screenWidth = display.getWidth(); on API < 13 
int screenHeight = size.y; // int screenHeight = display.getHeight(); on API <13 

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(something * screenWidth, something * screenHeight); // This should set the witdh and height of the TextView 
lp.setMargins(something * screenWidth, something * screenHeight, 0, 0); // This serves as the settings for the left and top position 

txtV.setHeight(something * screenHeight); 
txtV.setWidths(something * screenWidth); 
txtV.setLayoutParams(lp); 
0

提供每個value-sw600dp,value-sw720dp,value-hdpi,value-tvdpifor example)文件夾diff dimens.xml files,使您的佈局將根據屏幕size.Hope這可以幫助你挑選值。