Android橫豎屏總結

2021-06-16 20:37:34 字數 1341 閱讀 5454

橫豎屏切換後activity會重新執行oncreat函式,但是在android工程的mainfest.xml中加入android:screenorientation="user" android:configchanges="orientation|keyboardhidden"之後,橫豎屏切換之後就不會去執行oncreat函式了,而是會去呼叫onconfigurationchanged(),這樣我們就能控制橫豎屏的切換了。

或者在res目錄下建立layout-land和layout-port目錄,相應的layout檔案不變。layout-land是橫屏的layout,layout-port是豎屏的layout。

如果想讓它一直是橫屏顯示的話,只要在配置檔案中設定android:screenorientation="landscape"就行了,或者在**中 setrequestedorientation(activityinfo.screen_orientation_landscape);。如果想讓它一直是豎屏顯示的話,只要在配置檔案中設定android:screenorientation="portrait"就行了,或者在**中setrequestedorientation(activityinfo.screen_orientation_portrait);。

啟動的時候是橫屏的話就橫屏表示,縱屏的話就縱屏表示,手機切換橫豎屏不能用。

第一步:在mainfest.xml中追加android:screenorientation="sensor" android:configchanges="orientation|keyboardhidden"

第二步:取得螢幕的長和寬,進行比較設定橫豎屏的變數。

display display = getwindowmanager().getdefaultdisplay();

int width = display.getwidth();

int height = display.getheight();

if (width > height) else

第三步:在onconfigurationchanged()函式中追加this.setrequestedorientation(morientation)就行了

@override

public void onconfigurationchanged(configuration newconfig)

切到別的畫面的時候再回到原畫面,它就仍然是橫的或者是縱的。想讓它從別的螢幕回來後,又重新橫豎屏布局,只要在onresume()中在設定下就行了,但是這個只支援橫豎屏只有乙個layout的。

Android橫豎屏總結

橫豎屏切換後 activity 會重新執行 oncreate 函式,但是在 android 工程的 mainfest.xml 中加入 android screenorientation user android configchanges orientation keyboardhidden 之後,...

android 橫豎屏切換

private onclicklistener onclick new onclicklistener else 監聽系統設定的更改 override public void onconfigurationchanged configuration newconfig 在4.0以後需要加上scree...

Android 橫豎屏切換

今天老闆讓我看看有沒有辦法能讓我的背景桌布自己橫豎螢幕切換,今天發表下研究的成果。1.首先有時候開發的時候需要強制橫屏或者豎屏的時候,只需要在androidmanifest.xml中設定activity的android screenorientatation屬性就行了。unspecified 預設值...