获取Android中的屏幕高度 [英] Get the screen height in Android

查看:98
本文介绍了获取Android中的屏幕高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能在Android的屏幕可用高度?我需要的高度减去状态栏/菜单栏或其他任何装饰,可能是在屏幕上,我需要它适用于所有的设备。另外,我需要知道这一点在OnCreate函数。我知道这个问题已经被问过,但我已经尝试过他们的解决方案,其中没有工作。这里有一些事情,我曾尝试:

How can I get the available height of the screen in Android? I need to the height minus the status bar / menu bar or any other decorations that might be on screen and I need it to work for all devices. Also, I need to know this in the onCreate function. I know this question has been asked before but I have already tried their solutions and none of them work. Here are some of the things I have tried:

我已经测试了这个code对API 7 - 17。不幸的是,API的13有在底部额外的空间水平和垂直方向和API的10,8,7没有足够的空间在底部两个水平和垂直方向。 (我还没有过时的API的测试):

I have tested this code on API 7 - 17. Unfortunately, on API 13 there is extra space at bottom both horizontally and vertically and on API 10, 8, and 7 there is not enough space at the bottom both horizontally and vertically. (I have not tested on obsolete API's):

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;

TypedValue tv = new TypedValue();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
    if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        screenHeight -= TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
    screenHeight -= getResources().getDimensionPixelSize(resourceId);

这并没有考虑到状态栏/菜单栏:

This does not take into account the status bar / menu bar:

Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();

无论是做这个的:

Neither does this:

Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;

这也不:

Point size = new Point();
getWindowManager().getDefaultDisplay().getRealSize(size);
screenWidth = size.x;
screenHeight = size.y;

这不工作:

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
// since SDK_INT = 1;
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
try
{
    // used when 17 > SDK_INT >= 14; includes window decorations (statusbar bar/menu bar)
    screenWidth = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
    screenHeight = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
}
catch (Exception ignored)
{
    // Do nothing
}
try
{
    // used when SDK_INT >= 17; includes window decorations (statusbar bar/menu bar)
    Point realSize = new Point();
    Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
    screenWidth = realSize.x;
    screenHeight = realSize.y;
}
catch (Exception ignored)
{
    // Do nothing
}

然后我用下面的code减去从屏幕高度的状态栏和菜单栏的高度:

I then used the following code to subtract the height of the status bar and menu bar from the screen height:

int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
    result = getResources().getDimensionPixelSize(resourceId);
screenHeight -= result;
result = 0;
if (screenHeight >= screenWidth)
    resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
else
    resourceId = getResources().getIdentifier("navigation_bar_height_landscape", "dimen", "android");
if (resourceId > 0)
    result = getResources().getDimensionPixelSize(resourceId);
screenHeight -= result;

在API 17它正确地计算了状态栏和菜单栏在纵向的高度,但不是在景观。在API 10,则返回0。我需要它理想的工作,在所有设备上或最小的API 7.任何帮助将是很大的AP preciated。

On API 17 it correctly calculates the height of the status bar and menu bar in portrait but not in landscape. On API 10, it returns 0. I need it to work ideally on all devices or minimum API 7. Any help would be greatly appreciated.

推荐答案

工作方案:

创建两个虚拟的观点:

 <View
    android:id="@+id/top"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_alignParentTop="true" />

<View
        android:id="@+id/bottom"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true" />

现在,你应该叫 getLocationOnScreen将(INT [])这两个意见,接受他们的位置。该函数返回两个整数数组。第一个是x和第二Y。我们只需要年。还要注意的是正确的值返回后,才创建了所有的意见和的onCreate 办法退出。

Now, you should call getLocationOnScreen(int []) on these two views to receive their locations. This function returns an array of 2 integers. The first being x and the second y. We require only y. Also note that correct values are returned only AFTER all the views are created and the onCreate method is exited.

我经常片段从viewpager称为测试了这个code。

I tested this code on a regular fragment called from a viewpager.

在code在我片段 onCreateView 方法。我仍然不得不使用 postDelayed ,以获得正确的值。

The code was in the onCreateView method of my fragment. I still had to use a postDelayed to get the correct values.

final View top = (View) view.findViewById(R.id.top);
    final View bottom = (View) view.findViewById(R.id.bottom);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            int topLoc[] = new int[2];
            top.getLocationOnScreen(topLoc);
            int BottomLoc[] = new int[2];
            bottom.getLocationOnScreen(BottomLoc);
            Log.d(Constants.debug, "topY: "+ topLoc[1]+" BottomY:" + BottomLoc[1]);
        }
    },4000);

我有一台Nexus 4。

I have a Nexus 4.

在普通模式(状态栏显示的软按钮):

In regular mode (status bar and the soft buttons shown) :

07-05 02:28:23.367      565-565/com.torcellite.xx D/xx: topY: 50 BottomY:1182

在全屏模式(无状态栏或软按钮所示):

In full screen mode (no status bar or soft buttons shown) :

07-05 02:29:17.360      707-707/com.torcellite.xx D/xx: topY: 0 BottomY:1280

所有你现在要做的是减法。

All you have to do now is subtract.

希望有所帮助。干杯。

这篇关于获取Android中的屏幕高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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