2016-10-22 111 views
0

我正在製作尺子應用程序,並且我希望我的某個視圖在任何屏幕上的寬度都爲1.7英寸,而不考慮個人的dpi使用該應用的設備。我知道如何在像素和dp之間進行轉換,以及如何以編程方式設置視圖的佈局參數,但我不知道如何確定在每個屏幕密度下需要多少個dp以確保我的視圖總是以寬度繪製1.7英寸。Android:以英寸爲單位設置視圖的寬度(在英寸和dp之間轉換)

除非我記錯了,一旦我明白這種轉換,我應該可以設置此使用這樣一個電話:

RelativeLayout.LayoutParams viewParams = new RelativeLayout.LayoutParams(**width in dp converted from inches**, RelativeLayout.LayoutParams.MATCH_PARENT); 

回答

1

我建議這樣的:

public int pixelsOfWantedInches(double inches, Activity act) { 
     DisplayMetrics dm = new DisplayMetrics(); 
     act.getWindowManager().getDefaultDisplay().getMetrics(dm); 
     int dens=dm.densityDpi; 
     int pixels = (int)(inches * (double) dens); 

     return pixels; 
    } 


int wantedPixels = pixelsOfWantedInches(1.7, this); 
相關問題