在Android中检测屏幕大小
如果Android设备的屏幕尺寸被认为很小(320dp x 426dp单位),我需要禁用应用程序中的某个按钮。我目前正在使用:在Android中检测屏幕大小,android,screen-size,Android,Screen Size,如果Android设备的屏幕尺寸被认为很小(320dp x 426dp单位),我需要禁用应用程序中的某个按钮。我目前正在使用: if (activity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) { //Is small size } else { //Is
if (activity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
//Is small size
} else {
//Is not small size
}
但是,要确定设备的屏幕大小,这似乎从未进入配置的条件。屏幕布局\u size\u SMALL
。我做了打印,发现这个值实际上等于Configuration.SCREENLAYOUT\u SIZE\u NORMAL
Alex Lockwood的回答是:这是我对这个解决方案的参考,但正如J R p在他的回答中所指出的,一切似乎都等同于配置
如何可靠地定义设备的屏幕大小?我目前正在三星Galaxy S3上测试,无论如何这应该是一款“小型”设备
提前感谢,,
Rei使用以下代码
public boolean isPhone(Context activityContext) {
if(null!=activityContext)
{
boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((activityContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large)?false:true;
}
return false;
}
使用下面的代码
public boolean isPhone(Context activityContext) {
if(null!=activityContext)
{
boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((activityContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large)?false:true;
}
return false;
}
这里有两个函数来获取屏幕大小W和H
public static int getScreenSizeW(Context context) {
int screenSizeW = 320;//default
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
try {
screenSizeW = DisplayMetrics.class.getField("widthPixels").getInt(displayMetrics);
} catch (Exception e) {
}
return screenSizeW;
}
public static int getScreenSizeH(Context context) {
int screenSizeH = 480;//default
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
try {
screenSizeH = DisplayMetrics.class.getField("heightPixels").getInt(displayMetrics);
} catch (Exception e) {
}
return screenSizeH;
}
下面是获取显示大小的另一种方法:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
size.x = display.getWidth();
size.y = display.getHeight();
这里有两个函数来获取屏幕大小W和H
public static int getScreenSizeW(Context context) {
int screenSizeW = 320;//default
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
try {
screenSizeW = DisplayMetrics.class.getField("widthPixels").getInt(displayMetrics);
} catch (Exception e) {
}
return screenSizeW;
}
public static int getScreenSizeH(Context context) {
int screenSizeH = 480;//default
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
try {
screenSizeH = DisplayMetrics.class.getField("heightPixels").getInt(displayMetrics);
} catch (Exception e) {
}
return screenSizeH;
}
下面是获取显示大小的另一种方法:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
size.x = display.getWidth();
size.y = display.getHeight();
尝试以像素为单位显示尺寸
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
如需了解更多信息,请使用此按钮以像素为单位显示尺寸
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
有关更多信息,请使用此代码
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();
试试这段代码
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();
与其执行上述操作,不如为ldpi和mdpi创建不同的布局?在ldpi中,您将不包括按钮,而在代码中,您将检查该按钮是否存在于布局中(您的代码中缺少。因为我看到的不是屏幕像素密度,更多的是屏幕大小…与其执行上述操作,不如为ldpi和mdpi创建不同的布局?在ldpi中,您将不包括按钮,并且在代码中,您将检查该按钮是否存在于布局中。存在(代码中缺少。因为我看到的不是屏幕像素密度,更多的是屏幕大小。。。