Android屏幕大小的改变方法多种多样,可以根据用户不同的需求来进行更改。我们在这篇文章中介绍的就是其中一个自适应更改的方法。
Android应用程序中屏幕大小的设置大家应该都比较清楚,不过如何才能让屏幕自己适应环境而改变大小呢?在这里我们就可以为大家详细介绍一下有关Android屏幕大小的自适应方式,帮助大家理解。
不同的Android target会有不同的大小,应用程序的界面需要针对不同的大小调整界面元素的尺寸。而且Android屏幕大小也可以在横屏和竖屏之间切换,界面也需要调整。
如何取得屏幕的方向:
默认情况下,当屏幕方面切换时,activity的onCreate()方法会被重新调用,所以可以在其中通过以下代码来读取屏的方向:
- view plaincopy to clipboardprint?
- public void onCreate() {
- if(this.getResources().getConfiguration()
.orientation == Configuration.ORIENTATION_LANDSCAPE) { - Log.i("info", "landscape");
- } else if (this.getResources().getConfiguration()
.orientation == Configuration.ORIENTATION_PORTRAIT) { - Log.i("info", "portrait");
- }
- }
- public void onCreate() {
- if(this.getResources().getConfiguration()
.orientation == Configuration.ORIENTATION_LANDSCAPE) { - Log.i("info", "landscape");
- } else if (this.getResources().getConfiguration()
.orientation == Configuration.ORIENTATION_PORTRAIT) { - Log.i("info", "portrait");
- }
- }
如果在androidmanifest.xml中加入配置
- android:configChanges="orientation|keyboardHidden|navigation
当屏幕翻转时,Activity就不会重复的调用onCreate()、onPause()和onResume().
而是调用onConfigurationChanged(Configuration newConfig)
如何取得Android屏幕大小:
- view plaincopy to clipboardprint?
- int screenWidth,screenHeight;
- WindowManager windowManager = getWindowManager();
- Display display = windowManager.getDefaultDisplay();
- screenWidth = display.getWidth();
- screenHeight = display.getHeight();
- int screenWidth,screenHeight;
- WindowManager windowManager = getWindowManager();
- Display display = windowManager.getDefaultDisplay();
- screenWidth = display.getWidth();
- screenHeight = display.getHeight();
也有人提到另一种Android屏幕大小的更改方法:
- view plaincopy to clipboardprint?
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- int screenWidth = dm.widthPixels;
- int screenHeight = dm.heightPixels;
【编辑推荐】
posted on 2011-06-28 00:10
特务小强 阅读(65)
评论(0) 编辑 收藏