How to control landscape and portrait programmatically in android?

前端 未结 5 907
慢半拍i
慢半拍i 2021-02-06 07:07

I developed on application using android sdk 4.0 and I install that .apk file in my samsung tab. When I run that application it is working properly. If I change the tab portra

相关标签:
5条回答
  • 2021-02-06 07:12

    add android:screenOrientation="portrait" for each activity in your manifest.xml file.

    You can do this programmatically too, for all your activities making an AbstractActivity that all your activities extends.:-

    public abstract class AbstractActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
    
    0 讨论(0)
  • 2021-02-06 07:19

    If you search first, you'll find this question answered already. I'll post the answer again here though.

    Go to your manifest file, and under the activity that you want to keep portrait only, insert the following line
    android:screenOrientation="portrait" An example is given below.

    <activity android:name=".YourActivity"
          android:label="Your Activoity"
          android:screenOrientation="portrait">
    

    this will make your activity forced to be in portrait mode only, even if you hold the device in landscape mode.

    0 讨论(0)
  • 2021-02-06 07:34

    You can define mode in AndroidManifest.xml as follows,

    android:screenOrientation="portrait" after the android:name=".activityName"

    for example like this,

    <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait">
    

    and from activity class you can use following code,

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    0 讨论(0)
  • 2021-02-06 07:35

    In your manifest file,

    <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait">
    

    Then add this to your java class:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    0 讨论(0)
  • 2021-02-06 07:39

    You can use this for landscape

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    

    To change to portrait mode, use the

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    0 讨论(0)
提交回复
热议问题