问题
Help.I found the height of ListView and I do not know px or dpi? I need dpi
final ListView actualListView = mPullRefreshListView.getRefreshableView();
actualListView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
height = actualListView.getHeight();
}
});
回答1:
getheight return height in pixels, Below is what docs says..
public final int getHeight ()
Since: API Level 1
Return the height of your view. Returns
The height of your view, in pixels.
You need to convert px into dp , use below ways to convert it to dp.
Convert pixel to dp:
public int pxToDp(int px) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return dp;
}
or if you want it in px use below.
Convert dp to pixel:
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
回答2:
It returns pixels. http://developer.android.com/reference/android/view/View.html#getHeight() To convert pixels to dpi use this formula px = dp * (dpi / 160)
回答3:
Using this code you can get runtime Display's Width & Height
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
来源:https://stackoverflow.com/questions/11862391/getheight-px-or-dpi