Android:获取屏幕分辨率/像素作为整数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2902640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Get the screen resolution / pixels as integer values
提问by poeschlorn
this is a really simple question on which I've found no answer :/ How can I quickly access the screen resolution (width, height) as integer values?
这是一个非常简单的问题,我没有找到答案:/如何以整数值快速访问屏幕分辨率(宽度、高度)?
I've tried this one, but it always shows zero on my emulator:
我试过这个,但它在我的模拟器上总是显示为零:
DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels / 2;
In my case I want to dynamically create a table with tableRows, each containing two cols. This cols all shall fill half of the screen in width.
在我的情况下,我想用 tableRows 动态创建一个表,每个表包含两个列。这个 cols 都应填满屏幕的一半宽度。
Can someone give me a hint?
有人可以给我一个提示吗?
采纳答案by kgiannakakis
The easiest way will be to implement onSizeChanged. You are probably getting a value of 0 because the view hasn't initialized yet. You can't call the above code in the View constructor for example.
最简单的方法是实现onSizeChanged。您可能会得到 0 值,因为视图尚未初始化。例如,您不能在 View 构造函数中调用上述代码。
回答by Guillaume Perrot
The trashkalmar answer is correct.
垃圾卡尔马的答案是正确的。
However, the results will be specific to the activity context. If you want the whole device screen resolution:
但是,结果将特定于活动上下文。如果您想要整个设备的屏幕分辨率:
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
Note that the results also depend on the current device orientation.
请注意,结果还取决于当前的设备方向。
回答by trashkalmar
You can get screen metrics in this way:
您可以通过以下方式获取屏幕指标:
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
回答by DeepakPanwar
1.Simply use This inside activity to get screen width and height pixels.
1.简单地使用这个内部活动来获取屏幕宽度和高度像素。
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
2.This can also be used But requires Api level inlined check
2.这个也可以用但是需要Api级别的内联检查
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(size);
int width2 = size.x;
int height2 = size.y;
} else {
int width2 = display.getWidth();
int height2 = display.getHeight();
}
3.Use This when you are not in activity but having context
3.当你没有活动但有上下文时使用这个
WindowManager wm = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
Display display1 = wm.getDefaultDisplay();
Point size1 = new Point();
int width1;
int height1;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display1.getSize(size1);
width1 = size1.x;
height1 = size1.y;
} else {
width2 = display.getWidth();
height2 = display.getHeight();
}
回答by Rich
I use the following:
我使用以下内容:
int scrWidth = getWindowManager().getDefaultDisplay().getWidth();
int scrHeight = getWindowManager().getDefaultDisplay().getHeight();
No muss, no fuss, just 2 class variables
没有麻烦,没有大惊小怪,只有 2 个类变量