程序在动态布局时总要获得屏幕的大小来合理分配各个组件。大小及方向获得如下:
首先需要引入
import android.util.DisplayMetrics;
import android.view.WindowManager;
// 获得屏幕大小1
WindowManager manager = getWindowManager();
int width = manager.getDefaultDisplay().getWidth();
int height = manager.getDefaultDisplay().getHeight();
// 获得屏幕大小2
DisplayMetrics dMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dMetrics);
int screenWidth = dMetrics.widthPixels;
int screenHeight = dMetrics.heightPixels;
// 屏幕方面切换时获得方向
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setTitle("landscape");
}
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setTitle("portrait");
}