如何在android中以编程方式获取ScreenSize
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11252067/
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
How do I get the ScreenSize programmatically in android
提问by user1454356
Android defines screen sizes as Normal Large XLarge etc.
Android 将屏幕尺寸定义为 Normal Large XLarge 等。
It automatically picks between static resources in appropriate folders. I need this data about the current device in my java code. The DisplayMetrics only gives information about the current device density. Nothing is available regarding screen size.
它会自动在适当文件夹中的静态资源之间进行选择。我的 java 代码中需要有关当前设备的这些数据。DisplayMetrics 仅提供有关当前设备密度的信息。没有关于屏幕尺寸的可用信息。
I did find the ScreenSize enum in grep code hereHowever this does not seem available to me for 4.0 SDK. Is there a way to get this information?
我确实在此处的grep 代码中找到了 ScreenSize 枚举, 但是对于 4.0 SDK,我似乎无法使用它。有没有办法获得这些信息?
回答by Alex Lockwood
Copy and paste this code into your Activity
and when it is executed it will Toast
the device's screen size category.
将此代码复制并粘贴到您的代码中Activity
,当它执行时,它将Toast
显示设备的屏幕尺寸类别。
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
String toastMsg;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
toastMsg = "Large screen";
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
toastMsg = "Normal screen";
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
toastMsg = "Small screen";
break;
default:
toastMsg = "Screen size is neither large, normal or small";
}
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
回答by David
private static String getScreenResolution(Context context)
{
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
return "{" + width + "," + height + "}";
}
回答by Anshu P
Determine Screen Size :
确定屏幕尺寸:
int screenSize = getResources().getConfiguration().screenLayout &Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}
Determine density:
确定密度:
int density= getResources().getDisplayMetrics().densityDpi;
switch(density)
{
case DisplayMetrics.DENSITY_LOW:
Toast.makeText(context, "LDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_MEDIUM:
Toast.makeText(context, "MDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_HIGH:
Toast.makeText(context, "HDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_XHIGH:
Toast.makeText(context, "XHDPI", Toast.LENGTH_SHORT).show();
break;
}
for Ref: http://devl-android.blogspot.in/2013/10/wifi-connectivity-and-hotspot-in-android.html
参考:http: //devl-android.blogspot.in/2013/10/wifi-connectivity-and-hotspot-in-android.html
回答by Randika Vishman
I think it is a pretty straight forward simple piece of code!
我认为这是一段非常简单的代码!
public Map<String, Integer> deriveMetrics(Activity activity) {
try {
DisplayMetrics metrics = new DisplayMetrics();
if (activity != null) {
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
}
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("screenWidth", Integer.valueOf(metrics.widthPixels));
map.put("screenHeight", Integer.valueOf(metrics.heightPixels));
map.put("screenDensity", Integer.valueOf(metrics.densityDpi));
return map;
} catch (Exception err) {
; // just use zero values
return null;
}
}
This method now can be used anywhere independently. Wherever you want to get information about device screen do it as follows:
这种方法现在可以在任何地方独立使用。无论您想在何处获取有关设备屏幕的信息,请执行以下操作:
Map<String, Integer> map = deriveMetrics2(this);
map.get("screenWidth");
map.get("screenHeight");
map.get("screenDensity");
Hope this might be helpful to someone out there and may find it easier to use. If I need to re-correct or improve please don't hesitate to let me know! :-)
希望这可能对那里的人有所帮助,并且可能会发现它更易于使用。如果我需要重新纠正或改进,请不要犹豫让我知道!:-)
Cheers!!!
干杯!!!
回答by omor
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
回答by Aqif Hamid
You can get display size in pixels using this code.
您可以使用此代码以像素为单位获取显示大小。
Display display = getWindowManager().getDefaultDisplay();
SizeUtils.SCREEN_WIDTH = display.getWidth();
SizeUtils.SCREEN_HEIGHT = display.getHeight();
回答by Takermania
You can try this , It is working Example
你可以试试这个,它是有效的例子
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int ht = displaymetrics.heightPixels;
int wt = displaymetrics.widthPixels;
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG)
.show();
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(this,
"Screen size is neither large, normal or small",
Toast.LENGTH_LONG).show();
}
// Determine density
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
if (density == DisplayMetrics.DENSITY_HIGH) {
Toast.makeText(this,
"DENSITY_HIGH... Density is " + String.valueOf(density),
Toast.LENGTH_LONG).show();
} else if (density == DisplayMetrics.DENSITY_MEDIUM) {
Toast.makeText(this,
"DENSITY_MEDIUM... Density is " + String.valueOf(density),
Toast.LENGTH_LONG).show();
} else if (density == DisplayMetrics.DENSITY_LOW) {
Toast.makeText(this,
"DENSITY_LOW... Density is " + String.valueOf(density),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
this,
"Density is neither HIGH, MEDIUM OR LOW. Density is "
+ String.valueOf(density), Toast.LENGTH_LONG)
.show();
}
// These are deprecated
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
回答by Kevin Brey
simon-
西蒙-
Different screen sizes have different pixel densities. A 4 inch display on your phone could have more or less pixels then say a 26 inch TV. If Im understanding correctly he wants to detect which of the size groups the current screen is, small, normal, large, and extra large. The only thing I can think of is to detect the pixel density and use that to determine the actual size of the screen.
不同的屏幕尺寸具有不同的像素密度。手机上的 4 英寸显示屏可能具有更多或更少的像素,然后说 26 英寸电视。如果我理解正确,他想检测当前屏幕是哪个尺寸组,小、正常、大和特大。我唯一能想到的是检测像素密度并使用它来确定屏幕的实际大小。
回答by Tequilaman
I need this for a couple of my apps and the following code was my solution to the problem. Just showing the code inside onCreate. This is a stand alone app to run on any device to return the screen info.
我的几个应用程序需要这个,下面的代码是我解决问题的方法。只显示 onCreate 中的代码。这是一个独立的应用程序,可以在任何设备上运行以返回屏幕信息。
setContentView(R.layout.activity_main);
txSize = (TextView) findViewById(R.id.tvSize);
density = (TextView) findViewById(R.id.density);
densityDpi = (TextView) findViewById(R.id.densityDpi);
widthPixels = (TextView) findViewById(R.id.widthPixels);
xdpi = (TextView) findViewById(R.id.xdpi);
ydpi = (TextView) findViewById(R.id.ydpi);
Configuration config = getResources().getConfiguration();
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
txSize.setText("Large screen");
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG)
.show();
txSize.setText("Normal sized screen");
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG)
.show();
txSize.setText("Small sized screen");
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
Toast.makeText(this, "xLarge sized screen", Toast.LENGTH_LONG)
.show();
txSize.setText("Small sized screen");
} else {
Toast.makeText(this,
"Screen size is neither large, normal or small",
Toast.LENGTH_LONG).show();
txSize.setText("Screen size is neither large, normal or small");
}
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Log.i(TAG, "density :" + metrics.density);
density.setText("density :" + metrics.density);
Log.i(TAG, "D density :" + metrics.densityDpi);
densityDpi.setText("densityDpi :" + metrics.densityDpi);
Log.i(TAG, "width pix :" + metrics.widthPixels);
widthPixels.setText("widthPixels :" + metrics.widthPixels);
Log.i(TAG, "xdpi :" + metrics.xdpi);
xdpi.setText("xdpi :" + metrics.xdpi);
Log.i(TAG, "ydpi :" + metrics.ydpi);
ydpi.setText("ydpi :" + metrics.ydpi);
And a simple XML file
和一个简单的 XML 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tvSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/density"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/densityDpi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/widthPixels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/xdpi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/ydpi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
回答by Inzimam Tariq IT
If you are in a non-activity i.e. Fragment, Adapter, Model class or any other java class that do not extends Activity
simply getResources()
will not work. You can use getActivity()
in fragment or use context
that you pass to the corresponding class.
如果你是在IE碎片,适配器,型号类或任何其他Java类,做一个非活动不扩展Activity
根本getResources()
行不通。您可以getActivity()
在片段中使用或使用context
您传递给相应类的内容。
mContext.getResources()
I would recommend making a class say Utils that will have method/Methods for the common work. The benefit for this is you can get desired result with single line of code anywhere in the app calling this method.
我建议创建一个类,比如 Utils,它将具有用于常见工作的方法/方法。这样做的好处是您可以在调用此方法的应用程序中的任何位置使用单行代码获得所需的结果。