2013-07-10 71 views
0

如果Android设备的屏幕大小被视为小(320dp x 426dp单位),我需要禁用应用程序中的某个按钮。我目前正在使用:在Android中检测屏幕大小

if (activity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) { 
    //Is small size 
} else { 
    //Is not small size 
} 

,以确定设备的屏幕尺寸,但是这似乎从来没有进入Configuration.SCREENLAYOUT_SIZE_SMALL条件。我做了打印输出,发现这个值实际上相当于Configuration.SCREENLAYOUT_SIZE_NORMAL

Alex Lockwood在这里的答案是:How do I get the ScreenSize programmatically in android是我对这个解决方案的参考,但是正如J R P在他的回答中指出的那样,一切似乎都等同于我的Configuration.SCREENLAYOUT_SIZE_NORMAL

如何可靠地定义设备的屏幕尺寸?我目前正在三星Galaxy S3上进行测试,并且无论如何这应该是一个“小”设备。

由于提前, 川

+0

而不是做以上,你为什么不创建LDPI和MDPI不同的布局?在ldpi中,您不会包含按钮,并且在代码中您将检查该按钮是否存在于布局中。 – gunar

+0

有一个(在您的代码中缺少 – njzk2

+0

因为它不是屏幕像素密度我看着更多的屏幕尺寸... – Wakka02

回答

0

使用下面的代码。

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;  
} 
0

这是你可以用它来检查,如果你正在运行在智能手机应用程序,或使用它tablet.This是100%的功能的方法。

public boolean isNormalOrSmallResolution() { 
    boolean isNormalOrSmallResolution = false; 
    if (context != null) { 
     if (((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) 
       || ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL)) { 
      isNormalOrSmallResolution = true; 
     } 
    } 
    return isNormalOrSmallResolution; 
} 
1

来了两个函数来获取屏幕尺寸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(); 
4

尝试使用此方法显示尺寸像素

Display display = getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    int width = size.x; 
    int height = size.y; 

多使用此StackQuestion

1

试试这个代码..

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(); 

Also have a look at