android 横竖屏判断
android 横竖屏判断
1、在AndroidManifest.xml文件的activity中配置
android:screenOrientation="portrait";竖屏显示(高比宽要长)
android:screenOrientation="landscape":横屏显示(宽比高要长)
2、获取屏幕宽高度判断
Point point =new Point();
/*int width=getWindowManager().getDefaultDisplay().getWidth();
int height=getWindowManager().getDefaultDisplay().getHeight();*/
getWindowManager().getDefaultDisplay().getSize(point);
int width=point.x;
int height=point.y;
/*DisplayMetrics metrics =getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;*/
//竖屏
if(width<height){
//横屏ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
//竖屏ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
//设置成横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
3、在activity中重写onConfigurationChanged方法
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.orientation==getResources().getConfiguration().orientation){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
//setRequestedOrientation(requestedOrientation)
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。