Android:获取屏幕分辨率/像素作为整数值
这是一个非常简单的问题,我没有找到答案:/ 如何快速获取整数值的屏幕分辨率(宽度、高度)?
我已经尝试过这个,但它在我的模拟器上总是显示零:
DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels / 2;
在我的例子中,我想动态创建一个带有 tableRows 的表,每个表包含两个列。该列全部应填满屏幕宽度的一半。
有人可以给我提示吗?
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.
Can someone give me a hint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
垃圾卡尔玛的答案是正确的。
但是,结果将特定于活动上下文。如果您想要整个设备屏幕分辨率:
请注意,结果还取决于当前设备方向。
The trashkalmar answer is correct.
However, the results will be specific to the activity context. If you want the whole device screen resolution:
Note that the results also depend on the current device orientation.
您可以通过以下方式获取屏幕指标:
You can get screen metrics in this way:
1.只需使用此内部活动即可获取屏幕宽度和高度像素。
2.这也可以使用,但需要 Api 级别内联检查
3.当您不在活动中但有上下文时使用此
1.Simply use This inside activity to get screen width and height pixels.
2.This can also be used But requires Api level inlined check
3.Use This when you are not in activity but having context
我使用以下内容:
没有混乱,没有大惊小怪,只有 2 个类变量
I use the following:
No muss, no fuss, just 2 class variables
最简单的方法是实现 onSizeChanged。您可能会得到 0 值,因为视图尚未初始化。例如,您不能在 View 构造函数中调用上述代码。
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.