简体   繁体   中英

Landscape and Portrait view in android tablet

I am working on android. I will tried the following code to prevent the application to restart when the tab or phone is rotated. It is working but it does not give the landscape and portrait view correctly.

android:configChanges="keyboardHidden|orientation"

Actually,

          android:configChanges="orientation"
          android:screenOrientation="landscape"

attributes of an Activity declaration in the Manifest doesn't prevent the activity from being recreated whenever orientation changes, it prevents the platform from doing anything to the orientation by default and keeps it by default eg landscape.

You can override

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.newLayout);
}

to force recreation of the activity.

If you have 2 layouts (portrait and landscape) and the order seems to be reversed on the tablet then switch to using getRotation instead of deprecated getOrientation . Something like this

private void setLayout() {
    // Get display for detecting the phone orientation
    final Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    if (display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180) {
        setContentView(R.layout.home);
    } else {
        setContentView(R.layout.home_l);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM