Android屏幕大小相關(guān)技巧應(yīng)用指南
Android應(yīng)用程序中屏幕大小的設(shè)置大家應(yīng)該都比較清楚,不過如何才能讓屏幕自己適應(yīng)環(huán)境而改變大小呢?在這里我們就可以為大家詳細(xì)介紹一下有關(guān)Android屏幕大小的自適應(yīng)方式,幫助大家理解。
不同的Android target會(huì)有不同的大小,應(yīng)用程序的界面需要針對(duì)不同的大小調(diào)整界面元素的尺寸。而且Android屏幕大小也可以在橫屏和豎屏之間切換,界面也需要調(diào)整。
如何取得屏幕的方向:
默認(rèn)情況下,當(dāng)屏幕方面切換時(shí),activity的onCreate()方法會(huì)被重新調(diào)用,所以可以在其中通過以下代碼來讀取屏的方向:
- 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
當(dāng)屏幕翻轉(zhuǎn)時(shí),Activity就不會(huì)重復(fù)的調(diào)用onCreate()、onPause()和onResume().
而是調(diào)用onConfigurationChanged(Configuration newConfig)
如何取得Android屏幕大?。?/p>
- 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;
【編輯推薦】