Answers:
使用此代码,您可以获得运行时显示的宽度和高度:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
在视图中,您需要执行以下操作:
((Activity) getContext()).getWindowManager()
.getDefaultDisplay()
.getMetrics(displayMetrics);
在某些情况下,设备具有导航栏,则必须在运行时检查:
public boolean showNavigationBar(Resources resources)
{
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && resources.getBoolean(id);
}
如果设备具有导航栏,请计算其高度:
private int getNavigationBarHeight() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
return 0;
}
因此,设备的最终高度为:
int height = displayMetrics.heightPixels + getNavigationBarHeight();
有一个非常简单的答案,没有通过上下文
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
注意:如果要让高度包括导航栏,请使用以下方法
WindowManager windowManager =
(WindowManager) BaseApplication.getApplication().getSystemService(Context.WINDOW_SERVICE);
final Display display = windowManager.getDefaultDisplay();
Point outPoint = new Point();
if (Build.VERSION.SDK_INT >= 19) {
// include navigation bar
display.getRealSize(outPoint);
} else {
// exclude navigation bar
display.getSize(outPoint);
}
if (outPoint.y > outPoint.x) {
mRealSizeHeight = outPoint.y;
mRealSizeWidth = outPoint.x;
} else {
mRealSizeHeight = outPoint.x;
mRealSizeWidth = outPoint.y;
}
只是为了通过parag和SpK更新答案,以与不推荐使用的方法的当前SDK向后兼容性保持一致:
int Measuredwidth = 0;
int Measuredheight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
w.getDefaultDisplay().getSize(size);
Measuredwidth = size.x;
Measuredheight = size.y;
}else{
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();
}
getSize
Samsung Galaxy Mini(API 10)的实现中获取非零返回值,但是此答案中不建议使用的方法正确返回。因此,这对于旧版本的Android很有用。
为什么不
DisplayMetrics displaymetrics = getResources()。getDisplayMetrics();
然后使用
displayMetrics.widthPixels(heightPixels)
试试下面的代码:-
1。
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
2。
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
要么
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
3。
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.heightPixels;
metrics.widthPixels;
在Android中很容易获得:
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int height = Resources.getSystem().getDisplayMetrics().heightPixels;
对于Kotlin用户
fun Activity.displayMetrics(): DisplayMetrics {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
return displayMetrics
}
在Activity中,您可以像
resources.displayMetrics.let { displayMetrics ->
val height = displayMetrics.heightPixels
val width = displayMetrics.widthPixels
}
或片段
activity?.displayMetrics()?.run {
val height = heightPixels
val width = widthPixels
}
DisplayMetrics dimension = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dimension);
int width = dimension.widthPixels;
int height = dimension.heightPixels;
获取屏幕宽度和高度的值。
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
Display display = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int mWidthScreen = display.getWidth();
int mHeightScreen = display.getHeight();
public class DisplayInfo {
int screen_height=0, screen_width=0;
WindowManager wm;
DisplayMetrics displaymetrics;
DisplayInfo(Context context) {
getdisplayheightWidth(context);
}
void getdisplayheightWidth(Context context) {
wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
displaymetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displaymetrics);
screen_height = displaymetrics.heightPixels;
screen_width = displaymetrics.widthPixels;
}
public int getScreen_height() {
return screen_height;
}
public int getScreen_width() {
return screen_width;
}
}
对于Chrome OS的多显示器或即将推出的可折叠产品,此处的答案均无法正常使用。
查找当前配置时,请始终使用中来自您当前活动的配置
getResources().getConfiguration()
。不要使用后台活动中的配置或系统资源中的配置。后台活动没有大小,并且系统的配置可能包含大小和方向冲突的多个窗口,因此无法提取可用数据。
所以答案是
val config = context.getResources().getConfiguration()
val (screenWidthPx, screenHeightPx) = config.screenWidthDp.dp to config.screenHeightDp.dp
此处显示的方法已弃用/过时,但仍可以使用。需要API 13
一探究竟
Display disp= getWindowManager().getDefaultDisplay();
Point dimensions = new Point();
disp.getSize(size);
int width = size.x;
int height = size.y;
完整的方法,可以返回真正的分辨率:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
wm.getDefaultDisplay().getRealSize(size);
final int width = size.x, height = size.y;
由于这可以在不同的方向上更改,因此以下是一个解决方案(在Kotlin中),无论方向如何,它都可以正确实现:
/**
* returns the natural orientation of the device: Configuration.ORIENTATION_LANDSCAPE or Configuration.ORIENTATION_PORTRAIT .<br></br>
* The result should be consistent no matter the orientation of the device
*/
@JvmStatic
fun getScreenNaturalOrientation(context: Context): Int {
//based on : http://stackoverflow.com/a/9888357/878126
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val config = context.resources.configuration
val rotation = windowManager.defaultDisplay.rotation
return if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && config.orientation == Configuration.ORIENTATION_LANDSCAPE || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && config.orientation == Configuration.ORIENTATION_PORTRAIT)
Configuration.ORIENTATION_LANDSCAPE
else
Configuration.ORIENTATION_PORTRAIT
}
/**
* returns the natural screen size (in pixels). The result should be consistent no matter the orientation of the device
*/
@JvmStatic
fun getScreenNaturalSize(context: Context): Point {
val screenNaturalOrientation = getScreenNaturalOrientation(context)
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val point = Point()
wm.defaultDisplay.getRealSize(point)
val currentOrientation = context.resources.configuration.orientation
if (currentOrientation == screenNaturalOrientation)
return point
else return Point(point.y, point.x)
}
只需使用下面的函数即可将屏幕尺寸的宽度和高度作为整数数组返回
private int[] getScreenSIze(){
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int h = displaymetrics.heightPixels;
int w = displaymetrics.widthPixels;
int[] size={w,h};
return size;
}
在您的onCreate函数或按钮上,单击添加以下代码以输出屏幕尺寸,如下所示
int[] screenSize= getScreenSIze();
int width=screenSize[0];
int height=screenSize[1];
screenSizes.setText("Phone Screen sizes \n\n width = "+width+" \n Height = "+height);
试试Kotlin的此代码
val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
var DEVICE_WIDTH = size.x
var DEVICE_HEIGHT = size.y
我更新了Kotlin语言的答案!
对于Kotlin:您应该调用Window Manager并获取指标。在那简单的方法之后。
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
var width = displayMetrics.widthPixels
var height = displayMetrics.heightPixels
我们如何使用Kotlin语言以独立活动的方式有效地使用它?
在这里,我在Kotlin通用类中创建了一个方法。您可以在所有活动中使用它。
private val T_GET_SCREEN_WIDTH:String = "screen_width"
private val T_GET_SCREEN_HEIGHT:String = "screen_height"
private fun getDeviceSizes(activity:Activity, whichSize:String):Int{
val displayMetrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(displayMetrics)
return when (whichSize){
T_GET_SCREEN_WIDTH -> displayMetrics.widthPixels
T_GET_SCREEN_HEIGHT -> displayMetrics.heightPixels
else -> 0 // Error
}
}
联系人:@canerkaseler
我在此页面中找到weigan的最佳答案,这是如何在中使用它Xamarin.Android
:
public int GetScreenWidth()
{
return Resources.System.DisplayMetrics.WidthPixels;
}
public int GetScreenHeight()
{
return Resources.System.DisplayMetrics.HeightPixels;
}
屏幕分辨率是屏幕中像素总数。以下程序将提取设备的屏幕分辨率。它将打印屏幕的宽度和高度。这些值以像素为单位。
public static Point getScreenResolution(Context context) {
// get window managers
WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
// get width and height
int width = point.x;
int height = point.y;
return point;
}
这套实用程序可与Android中的Size抽象一起使用。
它包含一个SizeSizeDisplay.java类。 您可以像这样使用它:
ISize size = new SizeFromDisplay(getWindowManager().getDefaultDisplay());
size.width();
size.hight();
两步。首先,扩展Activity类
class Example extends Activity
第二:使用此代码
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager windowmanager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
windowmanager.getDefaultDisplay().getMetrics(displayMetrics);
int deviceWidth = displayMetrics.widthPixels;
int deviceHeight = displayMetrics.heightPixels;
int getScreenSize() {
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
// String toastMsg = "Screen size is neither large, normal or small";
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getRotation();
int i = 0;
switch (screenSize) {
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
i = 1;
// toastMsg = "Normal screen";
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
i = 1;
// toastMsg = "Normal screen";
break;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
// toastMsg = "Large screen";
if (orientation == Surface.ROTATION_90
|| orientation == Surface.ROTATION_270) {
// TODO: add logic for landscape mode here
i = 2;
} else {
i = 1;
}
break;
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
if (orientation == Surface.ROTATION_90
|| orientation == Surface.ROTATION_270) {
// TODO: add logic for landscape mode here
i = 4;
} else {
i = 3;
}
break;
}
// customeToast(toastMsg);
return i;
}
@Override
protected void onPostExecute(Drawable result) {
Log.d("width",""+result.getIntrinsicWidth());
urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 600);
// change the reference of the current drawable to the result
// from the HTTP call
urlDrawable.drawable = result;
// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
// For ICS
URLImageParser.this.container.setHeight((400+URLImageParser.this.container.getHeight()
+ result.getIntrinsicHeight()));
// Pre ICS`enter code here`
URLImageParser.this.container.setEllipsize(null);
}