Android Screen First off operation

Source: Internet
Author: User

Today handled a bit about Android screen of the effect, half a day down to think this screen of the relevant operation is still a lot of, nonsense end--------------

First look at the most common vertical and horizontal screen control, in the manifest file can be set whether the activity to support the screen, how to display (horizontal screen, vertical screen), the common way is to add in the activity

android:screenorientation= "XXX" so that you can limit how the activity is displayed.
<ActivityAndroid:name= ". UI. Detailsvideoactivity "android:hardwareaccelerated= "false"Android:label= "@string/app_name"android:screenorientation= "Portrait"android:configchanges= "Orientation|keyboardhidden|screensize"Android:theme= "@style/theme.pageindicatordefaults" >        </Activity>
android:screenorientation= "XXX", the property value of XXX
"Portrait" : Portrait display (Height > width)
"Landscape" : Horizontal display (Height < width) (both of which are more commonly used)
"Behind" : consistent with previous activity
"User": Consistent with users preferences
"Unspecified": Default, by System selection direction (uncertainty following system)
"Sensor" : Follow the device's direction sensor
"Fullsensor": Displays the four directions of the device sensor, so the activity will be displayed in this way after setting.
-----------------------------------------------------------------------------------------------------
The simplest way to set up, but when the screen from a horizontal screen to the tree screen, and the vertical screen to switch to the horizontal screen when the processing is often difficult for us, and it is here is hard to deal with. When the screen state is switched, it will usually recall the individual
Life cycle, which is equivalent to reloading the activity, this time the data will often disappear, but we can also save the state of the switch in the Activity class overwrite the onconfigurationchanged () method to save the state of the current activity (data)
1 @Override 2     protected void onsaveinstancestate (Bundle outstate) 3     {4         Super. Onsaveinstancestate (outstate); 5         outstate.putstring (Constant.rul_store, Webview.geturl ());  Save Data 6         System.out.println ("onsaveinstancestate"  + "---->" + webview.geturl ()) ; 7     }

You can then use the Onrestoreinstancestate method to obtain the previous state (data):

@Override     protected void onrestoreinstancestate (Bundle savedinstancestate)    {        super. Onrestoreinstancestate (savedinstancestate);         if (null! =savedinstancestate)        {            System.out.println ("onrestoreinstancestate"  + "---->" + savedinstancestate.getstring ( Constant.rul_store));             = savedinstancestate.getstring (constant.rul_store);        }    }

This allows you to save the activity's state (data) for use when switching between the screen and landscape.

Of course, sometimes we don't want the activity to re-invoke the life cycle when switching between screens, because it's likely to lose the state of the current activity, and then we'll add the Android:configchanges attribute to the activity node of the manifest file:

  < Activity             Android:name = "Com.lome.jy.MainActivity"             android:configchanges= "Keyboardhidden|orientation|screensize"            Android:label  = "@string/app_name">
Android:configchanges= "Keyboardhidden|orientation|screensize" plus this will not re-invoke the activity's life cycle when switching between them.

The effect of the Android:configchanges setting on the Activity toggle screen is still very large:

When you do not set the activity's android:configchanges, the screen will recall the various lifecycles, and will be executed once when the screen is cut, and twice when the vertical screen is cut.

When you set the activity's android:configchanges= "orientation", the screen will recall the life cycle, and it will only execute once when you cut the horizontal and vertical screen.

When you set the activity's android:configchanges= "orientation|keyboardhidden|screensize", the screen does not recall the various lifecycles. Only the Onconfigurationchanged method is executed

1 /**2 * onconfigurationchanged3 * the package:android.content.res.Configuration.4      * @paramNewconfig, the new device configuration.5 * When the device configuration information is changed (such as screen orientation changes, physical keyboard push or close, etc.),6 * And if there is activity running at this point, the system will call this function. 7 * Note: onconfigurationchanged will only monitor the application through the Anroidmainifest.xml8 * android:configchanges= "xxxx" to specify the configuration type of changes;9 * And for other configuration changes, the system OnDestroy () the current activity and then restarts a new activity instance. Ten      */ One @Override A      Public voidonconfigurationchanged (Configuration newconfig) { -         Super. onconfigurationchanged (newconfig); -         //to detect the orientation of the screen: portrait or Landscape the         if( This. Getresources (). GetConfiguration (). Orientation -==Configuration.orientation_landscape) {       atDisplaymetrics Displaysmetrics =Newdisplaymetrics (); -              This. Getwindowmanager (). Getdefaultdisplay (). Getmetrics (displaysmetrics); -             intPixwidth=Displaysmetrics.widthpixels; -             intheight=Displaysmetrics.heightpixels; -Linearlayout.layoutparams Mlayoutparams =NewLinearlayout.layoutparams ( -Pixwidth,height-20); in Webview.setlayoutparams (mlayoutparams); - //webview.reload (); to webview.refreshdrawablestate (); -         } the         Else if( This. Getresources (). GetConfiguration (). Orientation *==configuration.orientation_portrait) { $             //currently a vertical screen

Panax NotoginsengDisplaymetrics Displaysmetrics =Newdisplaymetrics (); - This. Getwindowmanager (). Getdefaultdisplay (). Getmetrics (displaysmetrics); the intPixwidth=Displaysmetrics.widthpixels; + intheight=Displaysmetrics.heightpixels; ALinearlayout.layoutparams Mlayoutparams =NewLinearlayout.layoutparams ( thePixwidth,height-20); + Webview.setlayoutparams (mlayoutparams); - webview.refreshdrawablestate (); $ } $ //detecting the state of the physical keyboard - if(Newconfig.hardkeyboardhidden -==configuration.hardkeyboardhidden_no) { the //physical keyboard is in the roll -out state - } Wuyi Else if(Newconfig.hardkeyboardhidden the==Configuration.hardkeyboardhidden_yes) { - //The physical keyboard is in a closed state Wu } -}

As a result, we can listen to the changes in the phone screen in the Onconfigurationchanged method so that we can do a lot of things, change the style, change the content ....

Alternatively, you can disable the horizontal screen (vertical screen) with the selective setting here:

Setrequestedorientation (activityinfo.screen_orientation_portrait);



Android Screen First off operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.