The Android APP simply and efficiently disables horizontal and vertical screen switching and androidapp

Source: Internet
Author: User

The Android APP simply and efficiently disables horizontal and vertical screen switching and androidapp

By default, the Android APP interface changes with the mobile phone direction. When the mobile phone is in the portrait screen status, the APP interface is also in the portrait screen status, when the mobile phone is in the landscape status, the APP automatically switches to the landscape status. In general, the APP interface is designed for the vertical screen. Once the screen is automatically switched to the horizontal screen, the interface may not be directly looked. In addition, the current page will be destroyed and re-created each time the screen is switched.

The following is a simple demonstration.

Layout file:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android: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 class OrientationActivity extends Activity {private TextView tvMsg; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_orientation); tvMsg = (TextView) find ViewById (R. id. tvMsg); // by default, the current Activity is destroyed each time the screen is switched horizontally and vertically, and then re-created, and the onCreate method is called. ShowOrientation ();} private void showOrientation () {if (getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE) {tvMsg. setText ("current screen");} else if (getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT) {tvMsg. setText ("Current landscape ");}}}

Run the following command:

When the screen is rotated to the horizontal screen:

You can disable horizontal/vertical screen switching in either of the following ways: Configure in the configuration file and set in Java code.

The first is in AndroidManifest. in xml, configure the android: screenOrientation attribute for the activity node and specify the activity as a portrait or landscape screen. We will configure the activity as a portrait screen, as shown below:

<activity            android:name="chengyujia.androidtest.OrientationActivity"            android:screenOrientation="portrait" />

Run the test again. At this time, regardless of the phone screen direction, the activity is always portrait. If android: screenOrientation = "landscape", the screen is always landscape.

The second type is shown below,

Just add a sentence in the onCreate Method

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

You can always keep the portrait screen. If you want to landscape screen, the code is

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

As shown above, it is very easy to disable horizontal and vertical screen switching for a single Activity, but there are many activities in the actual project. It is too troublesome to set each Activity. Is there a way to set global validity? The answer is yes. You only need to modify the second method slightly. We can write the following BaseActivity class:

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);    }}

So that other activities can inherit the BaseActivity class to enable global disable switching between landscape and landscape screens. This method is often used in actual development. It is not only used to set the landscape and landscape, but also other common functions can be written in BaseActivity. Inheritance is a good thing, haha.

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.