這篇文章主要講解了“Android中如何設(shè)置屏幕大小”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Android中如何設(shè)置屏幕大小”吧!
默認(rèn)情況下,當(dāng)屏幕方面切換時(shí),activity的onCreate()方法會(huì)被重新調(diào)用,所以可以在其中通過以下代碼來(lái)讀取屏的方向:
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屏幕大小:
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;
感謝各位的閱讀,以上就是“Android中如何設(shè)置屏幕大小”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Android中如何設(shè)置屏幕大小這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!