Monodroid Study Notes (14th) -- dynamically change the screen direction

Source: Internet
Author: User

The Android mobile phone has an interesting feature, that is, when you pass the mobile phone, the content of the mobile phone will also follow. How can I control the display direction of an activity through a program? In monodroid, to change the display direction of the screen through a program, you only need to call the setrequestedorientation method. To obtain the direction of the current screen, use the requestedorientation attribute.

This example is very simple. There is only one button on the interface. When you click it, you can determine the direction of the current screen. If it is a portrait screen (portrait), change it to landscape screen (landscape), and vice versa. The layout file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <button Android: Id = "@ + ID/btnlandscape" <br/> Android: TEXT = "rotate the screen by me" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"/> </P> <p> </relativelayout> 

The program is also very simple. You only need to judge the current screen direction in the Click Event of the button, and then call the setrequestedorientation method:

Protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> button btnlandscape = findviewbyid <button> (resource. id. btnlandscape); <br/> btnlandscape. click + = (sender, e) =>< br/>{< br/> If (this. requestedorientation = (INT) Android. content. PM. screenorientation. landscape) <br/>{< br/> This. setrequestedorientation (Android. content. PM. screenorientation. portrait); <br/> toast. maketext (this, "rotate to portrait screen", toastlength. long ). show (); <br/>}< br/> else if (this. requestedorientation = (INT) Android. content. PM. screenorientation. portrait) <br/>{< br/> This. setrequestedorientation (Android. content. PM. screenorientation. landscape); <br/> toast. maketext (this, "rotate to landscape screen", toastlength. long ). show (); <br/>}< br/>}; <br/>}< br/> 

Run the program in the simulator, click the button, then? Why not? I followed the code and found that this. requestedorientation turned out to be-1 !, What's the problem? It turns out that in Android, if we do not set the Android: screenorientation attribute of activity in androidmanifest. XML, this. requestedorientation defaults to-1 even if the screen is portrait when the program is running. However, do you remember, as we mentioned in the tenth note, androidmanifest in monodroid. in XML, the activity attribute can be implemented by marking the activity class attivity. Therefore, we only need to add the screenorientation attribute to the attribute of our activity1 class:

[Activity (Label = "monodroidtest", mainlauncher = true, screenorientation = Android. content. PM. screenorientation. Portrait)] <br/> public class activity1: activity <br/> 

Run the program again and click the button. The result is as follows:

If you do not want to add the screenorientation attribute to the activity, you can use another method to determine the current screen direction, that is, to determine the screen width and height. If the width is greater than the height, it is a horizontal screen, otherwise it is a vertical screen.

Protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> button btnlandscape = findviewbyid <button> (resource. id. btnlandscape); <br/> btnlandscape. click + = (sender, e) => <br/>{< br/> display D = This. window. windowmanager. defaultdisplay; <br/> int H = D. height; <br/> int W = D. width; <br/> If (W> H) <br/>{< br/> This. setrequestedorientation (Android. content. PM. screenorientation. portrait); <br/>}< br/> else <br/>{< br/> This. setrequestedorientation (Android. content. PM. screenorientation. landscape); <br/>}< br/>}; <br/>}< br/> 

The effect is the same. If we change the screen direction, is there any event triggered when the screen direction changes? Yes, we only need to override the onconfigurationchanged method. This method is triggered when the system settings change. The only input parameter is the configuration object, except for the Screen Settings change event, you can also handle other system configuration changes, such as hiding the keyboard or opening the keyboard.

Public override void onconfigurationchanged (Android. content. res. configuration newconfig) <br/>{< br/> display D = This. window. windowmanager. defaultdisplay; <br/> int H = D. height; <br/> int W = D. width; <br/> If (newconfig. orientation = android. content. res. orientation. landscape) <br/>{< br/> toast. maketext (this, String. format ("rotate to landscape screen, resolution: {0} × {1}", W, h), toastlength. long ). show (); <br/>}< br/> else if (newconfig. orientation = android. content. res. orientation. portrait) <br/>{< br/> toast. maketext (this, String. format ("rotate to portrait screen, resolution: {0} × {1}", W, h), toastlength. long ). show (); <br/>}< br/> base. onconfigurationchanged (newconfig); <br/>}< br/> 

Why is it because the program is still ineffective? To enable onconfigurationchanged to take effect, you must add the configurationchanges attribute on the activity and set the value to Android. content. PM. configchanges refers to a Union of enumerated values. For example, if we want to capture screen changes, we must assign them to Android. content. PM. configchanges. orientation. If you want to capture the keyboard change event at the same time, you must assign: Android. content. PM. configchanges. orientation | android. content. PM. configchanges. keyboard, as follows:

[Activity (Label = "monodroidtest", mainlauncher = true, configurationchanges = android. content. PM. configchanges. orientation | android. content. PM. configchanges. keyboard)] <br/> public class activity1: activity <br/> {<br/> 

We re-run the program, and the effect will come out:

Finally, let us know how to change the direction of the simulator. We can press Ctrl + F11 to simulate it:

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.