[英]Opening activity in landscape mode without locking the orientation
我想最初以橫向模式打開活動,但我不想鎖定該方向,這意味着用戶可以在旋轉時切換方向。我嘗試將以下代碼添加到活動中。它以橫向模式打開活動但已鎖定。
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
通過在活動中添加android:configChanges="orientation|keyboardHidden"
,您可以處理方向更改時的操作。
@override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//change orientation here
} else if (newConfig.orientation ==Configuration.ORIENTATION_LANDSCAPE{
//change orientation here
}
}
嘗試一下...在Android Manifest.xml的活動標簽中添加此行
android:screenOrientation="landscape"
如下所示:
<activity
android:name="com.abc.def.MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.