package com.kale.screen; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Point; import android.os.Bundle; import android.util.Log; import android.view.Display; public class MainActivity extends Activity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int dw = getWindowManager().getDefaultDisplay().getWidth();//获得手机屏幕的宽度 int dh = getWindowManager().getDefaultDisplay().getHeight();//获得手机屏幕的高度 Log.i("h", "height:"+dw); Log.i("w", "width:"+dh); //推荐的获取屏幕长宽的方式,但需要API13 Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int windowHeight = size.y; int windowWidth = size.x; Log.i("w","width:"+ windowWidth); Log.i("h", "height:"+ windowHeight); } }
Android中获取屏幕长宽的方法
时间: 2024-12-19 00:40:36