By default, Android app's interface will change with the direction of the phone, when the phone is in the vertical state, the app's interface is also in the vertical state, and when the phone is in a horizontal screen, the app will automatically switch to the horizontal screen state. In general, the app interface is designed for vertical screen, once automatically switch to the horizontal screen, the interface may not be able to look directly. And every time the screen is switched on, the current page will be destroyed and recreated.
Let's start with a simple demo.
Layout file:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/tvmsg"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" /></LinearLayout>
Background activity file:
Package Chengyujia.androidtest;import Android.app.activity;import android.content.res.configuration;import Android.os.bundle;import Android.widget.TextView; Public classOrientationactivity extends Activity {PrivateTextView tvmsg; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_orientation); Tvmsg=(TextView) Findviewbyid (r.id.tvmsg); //by default, the current activity is destroyed, recreated, and called the OnCreate method each time the screen is toggled. showorientation (); } Private voidshoworientation () {if(Getresources (). GetConfiguration (). Orientation = =Configuration.orientation_landscape) {Tvmsg.settext ("currently a horizontal screen"); } Else if(Getresources (). GetConfiguration (). Orientation = =configuration.orientation_portrait) {Tvmsg.settext ("currently a vertical screen"); } }}
Here's how to run:
When rotating the screen to a horizontal screen:
There are two ways to disable the portrait-to-landscape switch, the first one being configured in the configuration file and the second being set in Java code.
The first is to configure the Android:screenorientation property for the Activity node in Androidmanifest.xml, specify that the activity is a vertical or horizontal screen, and we will configure the activity above as a vertical screen, As follows:
< Activity Android:name = "Chengyujia.androidtest.OrientationActivity" android:screenorientation= "Portrait"/>
Run the test again, at which point the activity is always vertical regardless of the screen orientation of the phone. If android:screenorientation= "Landscape" is always a horizontal screen.
Here's a second,
Just add a sentence to the OnCreate method.
Setrequestedorientation (activityinfo.screen_orientation_portrait);
You can always keep the vertical screen, if you want to cross-screen, the code is
Setrequestedorientation (Activityinfo.screen_orientation_landscape);
As above, it is very easy for a single activity to disable the toggle screen, but the actual project will have a lot of activity, if each set up is too troublesome. Is there a way to set up a globally valid method? The answer is yes, just a little makeover on the second way. We can write a baseactivity class like this:
package Chengyujia.androidtest; import android.app.Activity; import Android.content.pm.ActivityInfo; import Android.os.Bundle; public class baseactivity extends Activity {@Override protected void OnCreate (Bundle savedinstancestate) { super.oncreate (Savedinstancestate); Setrequestedorientation (activityinfo.screen_orientation_portrait); }}
Allowing other activity to inherit this baseactivity class will enable the global disabling of the toggle screen. This method is commonly used in practical development, not only for setting up the screen, but also for other public functions that can be written in baseactivity. Inheritance is a good thing ah, haha.
Android APP is simple and efficient to disable portrait and screen switching