Android Portrait and landscape

五迷三道 提交于 2021-01-28 07:48:54

问题


I locked my app on portrait mode for mobile devices, and landscape for TV. The issue is, I want it on the whole app. I have multiple activities inside my app, so I have to check every activity for portrait and landscape. Is there a way to make my code global for all the activities? I don't want to write that code in every activity.

Here is my code:

I'm using this before:

setContentView(R.layout.activity_main);

MainActivity.java

// locking out landscape screen orientation for mobiles
if(getResources().getBoolean(R.bool.portrait_only)){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

// locking out portrait screen orientation for TV
if(getResources().getBoolean(R.bool.landscape_only)){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

回答1:


You can set the screenOrientation as 'behind' in Manifest file for all activities other than MainActivity.

This makes all the activities to keep the orientation same as that of behind activity. So if you have already locked the orientation for MainActivity, then all the activities started from MainActivity will keep same orientation of MainActivity.




回答2:


Create BaseActivity, add this function into it and your activities will extend BaseActivity.




回答3:


The positions of different Views appear different in Landscape and Portrait mode. If you are okay with the changed positions, then its fine. Otherwise, sometimes the positions are changed so dramatically that the layout does not appear good which is why you have to arrange the Views manually for both Portrait and Landscape mode.



来源:https://stackoverflow.com/questions/53221517/android-portrait-and-landscape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!