getheight()px或dpi? [英] getheight() px or dpi?

查看:72
本文介绍了getheight()px或dpi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助。我找到了ListView的高度,我不知道px或dpi吗?我需要dpi

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();  

                    }
                });


推荐答案

getheight返回高度(以像素为单位),下面是文档说的话。

getheight return height in pixels, Below is what docs says..

  public final int getHeight ()





由于:API级别1

Since: API Level 1

返回视图的高度。
返回

Return the height of your view. Returns

视图的高度,以像素为单位。

The height of your view, in pixels.


您需要将px转换为dp,使用以下方法将其转换为dp。

You need to convert px into dp , use below ways to convert it to dp.

将像素转换为dp:

public int pxToDp(int px) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return dp;
}

,或者如果您希望在下面的px中使用它。

or if you want it in px use below.

将dp转换为像素:

public int dpToPx(int dp) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));       
    return px;
}

这篇关于getheight()px或dpi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆