2022年3月13日 星期日

Android Studio - 鎖定橫屏或豎屏(Lock Screen)(六十四)

Android Studio - 鎖定橫屏或豎屏(Lock Screen)(六十四):

筆者編程時需要鎖定 LCD 屏幕為豎屏(Portrait Orientation)或橫屏(Landscape Orientation),禁止屏幕旋轉,方便觀看資料,但一般屏幕會自動根據傳感器的方向來改變方向,筆者不想停止傳感器功能,因為其它程式需要使用,所以編程便加入了禁止屏幕改變,固定在一個模式。設定橫屏豎屏是可以修改 AndroidManifest.xml 的屬性或編寫程式。

Android Studio的豎屏(Portrait Screen)和橫屏(Landscape Screen)
修改 AndroidManifest.xml - Lock Screen Orientation 控制屏幕方向:

xmlns:tools="http://schemas.android.com/tools"

android:screenOrientation = "portrait">

android:screenOrientation = "landscape">

tools:ignore="LockedOrientationActivity">

 

// 固定 橫向

<activity

    android:name=".module.orientation.ScreenOrientationActivity"

    android:screenOrientation="landscape" />

 

//固定 縱向

<activity

    android:name=".module.orientation.ScreenOrientationActivity"

    android:screenOrientation="portrait" />

 

//方向感測器

<activity

    android:name=".module.orientation.ScreenOrientationActivity"

    android:screenOrientation="sensor" />

 

// 固定 橫向

<activity

    android:name=".module.orientation.ScreenOrientationActivity"

    android:screenOrientation="landscape" />

 

 


修改程式 - Lock Screen Orientation 控制屏幕方向:

// Lock Screen Portrait Orientation豎屏顯示(高比寬要長) ,userPortrait跟它效果一樣就是豎屏而已

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

 

// Lock Screen Lanscape Orientation橫屏顯示(寬比高要長),豎屏向左或右旋轉90度;userLanscape,只豎屏旋轉90

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

 

// Screen Orientation by Sensor有物理的感應器來決定,一般設置的自動旋轉應該就是根據物理的感應器來相應的

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

 

//忽略物理感應器,這樣就不會隨著使用者旋轉設備而更改了

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

 

//和該Activity下面的那個Activity的方向一致(Activity堆疊中的)

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND);

 

//用戶當前首選的方向

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);

 

//預設值,不特別指定,由系統來判斷顯示方向

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);


Screen orientation 屏幕屬性:

            SCREEN_ORIENTATION_UNSET,

            SCREEN_ORIENTATION_UNSPECIFIED,

            SCREEN_ORIENTATION_LANDSCAPE,

            SCREEN_ORIENTATION_PORTRAIT,

            SCREEN_ORIENTATION_USER,

            SCREEN_ORIENTATION_BEHIND,

            SCREEN_ORIENTATION_SENSOR,

            SCREEN_ORIENTATION_NOSENSOR,

            SCREEN_ORIENTATION_SENSOR_LANDSCAPE,

            SCREEN_ORIENTATION_SENSOR_PORTRAIT,

            SCREEN_ORIENTATION_REVERSE_LANDSCAPE,

            SCREEN_ORIENTATION_REVERSE_PORTRAIT,

            SCREEN_ORIENTATION_FULL_SENSOR,

            SCREEN_ORIENTATION_USER_LANDSCAPE,

            SCREEN_ORIENTATION_USER_PORTRAIT,

            SCREEN_ORIENTATION_FULL_USER,

            SCREEN_ORIENTATION_LOCKED


2022年 3月 13日(Sun)天氣報告
氣溫:43.0°F / 6.0°C @ 07:00
風速:每小時 2公里
降雨機會:3%
相對濕度:百分之 88%
天氣:局部有雲

沒有留言:

張貼留言