Overview of Android horizontal and vertical screen switching and Android screen Switching

Source: Internet
Author: User

Overview of Android horizontal and vertical screen switching and Android screen Switching

Both Android phones and tablets have the function of switching between portrait and portrait screens, which is usually triggered by physical gravity sensing. However
Sometimes it is not the case. In the settings, we can disable the switching between the portrait and portrait screens of the mobile phone.

AndroidManifest. xml

<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.yanlei.yl7" >    <!-- Include required permissions for Google Mobile Ads to run. -->    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <!-- This meta-data tag is required to use Google Play Services. -->        <meta-data            android:name="com.google.android.gms.version"            android:value="@integer/google_play_services_version" />        <activity            android:name=".MainActivity"                  android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity> <!-- Include the AdActivity configChanges and theme. -->        <activity            android:name="com.google.android.gms.ads.AdActivity"            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"            android:theme="@android:style/Theme.Translucent" />    </application></manifest>

  

Activity_main.xml

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "get Configuration information" android: textSize = "25sp" android: layout_marginTop = "80dip" android: layout_centerHorizontal = "true"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "test ConfigurationChange" android: textSize = "25sp" android: layout_centerInParent = "true" android: id = "@ + id/mytext"/> </RelativeLayout>

MainActivity. java

Package com. example. yanlei. yl7; import android. content. res. configuration; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; import android. widget. toast; public class MainActivity extends AppCompatActivity {private Button mButton; private TextView pTextView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); pTextView = (TextView) this. findViewById (R. id. mytext); System. out. println ("---> onCreate ()"); init ();} private void init () {mButton = (Button) findViewById (R. id. button); mButton. setOnClickListener (new ClickListenerImpl ();} private class ClickListenerImpl implements OnClickListener {@ Override public void onClick (View v) {getConfigurationInfo () ;}} private void getConfigurationInfo () {Configuration configuration = getResources (). getConfiguration (); // get the int l = configuration in the screen direction. ORIENTATION_LANDSCAPE; int p = configuration. ORIENTATION_PORTRAIT; if (configuration. orientation = l) {pTextView. setText ("the current screen is landscape ===="); System. out. println ("current screen");} if (configuration. orientation = p) {pTextView. setText ("the current screen is vertical ="); System. out. println ("") ;}@ Override public void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); // newConfig. orientation to obtain the current screen status is horizontal or vertical // Configuration. ORIENTATION_PORTRAIT indicates vertical // Configuration. ORIENTATION_LANDSCAPE indicates the landscape if (newConfig. orientation = Configuration. ORIENTATION_PORTRAIT) {pTextView. setText ("current screen"); Toast. makeText (MainActivity. this, "Now is a portrait screen", Toast. LENGTH_SHORT ). show ();} if (newConfig. orientation = Configuration. ORIENTATION_LANDSCAPE) {pTextView. setText ("current screen"); Toast. makeText (MainActivity. this, "the current screen is landscape", Toast. LENGTH_SHORT ). show () ;}@ Override protected void onSaveInstanceState (Bundle outState) {super. onSaveInstanceState (outState); outState. putString ("name", "zxx"); outState. putInt ("id", 9527); System. out. println ("---> onSaveInstanceState ()") ;}@ Override protected void onRestoreInstanceState (Bundle savedInstanceState) {super. onRestoreInstanceState (savedInstanceState); String name = savedInstanceState. getString ("name"); int id = savedInstanceState. getInt ("id"); System. out. println ("---> onRestoreInstanceState ()"); System. out. println ("name =" + name + ", no. =" + id) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. menu_main, menu); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

 



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.