简体   繁体   English

如何在Android中获得USABLE屏幕宽度和高度

[英]How to get USABLE screen width and height in Android

I'm creating an app where I really need to know the correct screen dimensions. 我正在创建一个应用程序,我真的需要知道正确的屏幕尺寸。 Actually I know how to do that... but a problem appeared, because even if you do requestWindowFeature(Window.FEATURE_NO_TITLE); 其实我知道怎么做...但是出现了一个问题,因为即使你做了requestWindowFeature(Window.FEATURE_NO_TITLE); , this: , 这个: 在此输入图像描述

still stays on the screen and it seems, it's also counted as a part of the screen so the "screenWidth" is actually bigger, than the usable part. 仍然停留在屏幕上似乎,它也被视为屏幕的一部分,因此“screenWidth”实际上比可用部分更大。

Isn't there any method how to get the size of the usable part and not whole screen? 是不是有任何方法如何获得可用部分的大小而不是整个屏幕? Or at least get the sizes of the 'scrolling thing'(but there would be a problem, that there are devices that don't show them)? 或者至少得到'滚动的东西'的大小(但是会有问题,有些设备没有显示它们)?

I had the same question a while back and here is the answer that i found. 我不久前有同样的问题,这是我找到的答案。 Shows the activity dimensions. 显示活动维度。

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.graphics.Point;

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main)

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        int width = size.x;
        int height = size.y;

        //Set two textViews to display the width and height
        //ex: txtWidth.setText("X: " + width);
    }
}

I know this question is really old, but I have fought with this so many times until I found this stupidly simple solution: 我知道这个问题真的很老了,但是我已经和这个问题打了很多次,直到找到这个愚蠢的简单解决方案:

public final void updateUsableScreenSize() {
    final View vContent = findViewById(android.R.id.content);
    vContent.post(new Runnable() {
        @Override
        public void run() {
            nMaxScreenWidth = vContent.getWidth();
            nMaxScreenHeight = vContent.getHeight();
        }
    });
}

Run this code after setContentView() in your Activity's onCreate(), and delay any code that depends on those values until onCreate() is finished, for example by using post() again after this one. 在Activity的onCreate()中的setContentView()之后运行此代码,并延迟依赖于这些值的任何代码,直到onCreate()完成,例如在此之后再次使用post()。

Explanation: This code obtains your activity's root view, which can always be accessed by generic resource id android.R.id.content, and asks the UI handler to run that code as the next message after initial layout is done. 说明:此代码获取您的活动的根视图,该视图始终可以由通用资源ID android.R.id.content访问,并要求UI处理程序在初始布局完成后将该代码作为下一条消息运行。 Your root view will be of the exact size you have available. 您的根视图将具有您可用的确切大小。 You cannot run this directly on the onCreate() callback because layout did not happen yet. 你不能直接在onCreate()回调上运行它,因为布局还没有发生。

you can use also the full screen 你也可以全屏使用

developer.android developer.android

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM