简体   繁体   English

在Android 2.2上获取屏幕宽度和高度

[英]Get Screen width and height on android 2.2

How I can get screen width and height for use this value on device with android version 2.2(API lv8). 如何获取屏幕宽度和高度,以在具有Android 2.2(API lv8)版本的设备上使用此值。 I use this code: 我使用以下代码:

public static Point Maximum(Context context)
       {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        int width = display.getWidth();  // deprecated
            int height = display.getHeight();  // deprecated
            Point temp=new Point();     
            return temp;
       }

And when i want check by print Max point to textview: 当我想通过打印最大检查指向textview时:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView txt= (TextView)findViewById(R.id.text1);
    txt.setText(Math.Maximum(this).toString()); 
}

Point is (0,0). 点是(0,0)。 What problem? 什么问题?

Try this code. 试试这个代码。 But this API is deprecated in the new SDK versions you can use this. 但是在新的SDK版本中不推荐使用此API,您可以使用它。

Display display = activity.getWindowManager().getDefaultDisplay();
int width  = display.getWidth();
int height = display.getHeight();

Try this code: 试试这个代码:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

Try this for getting your screen width. 尝试此操作以获取屏幕宽度。 Similarly you can get screen height or can customize the same for returning both in a single function. 同样,您可以获取屏幕高度,也可以自定义屏幕高度以在单个函数中同时返回两者。

 @SuppressWarnings("deprecation")
 @SuppressLint("NewApi")
 private int getScreenWidth() {
    int width = 400;
    int height = 600; // just intialising it to default value in case if there is any problem getting screen width
    WindowManager wm = (WindowManager) activity // activity is the context if you are using this method in adapter. Otherwise in Activity you can simply ignore this
            .getSystemService(Context.WINDOW_SERVICE); 
    Display display = wm.getDefaultDisplay();
    if (android.os.Build.VERSION.SDK_INT >= 13) {
        Point size = new Point();
        display.getSize(size);
        width = size.x;
        height = size.y;
    } else {
        width = display.getWidth();
        height= display.getHeight();
    }
    return width;//or height
}

Get Device Width and Height, return as point. 获取设备宽度和高度,以点为单位返回。 Hope u like this. 希望你喜欢这样。

public static Point Maximum(){
    Display display = activity.getWindowManager().getDefaultDisplay();
    int width  = display.getWidth();
    int height = display.getHeight();
    Point temp=new Point();
    temp.set(width, height);     
    return temp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM