问题
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