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;
【編輯推薦】