Android横竖屏幕切换小结
Android手机或平板都会存在横竖屏切换的功能,通常是由物理重力感应触发的,但是有时候也不尽然,通常在设置里面我们可以对手机的横竖屏切换进行关闭。
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"/> <?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yanlei.yl7"> <!--IncluderequiredpermissionsforGoogleMobileAdstorun.--> <uses-permissionandroid:name="android.permission.INTERNET"/> <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permissionandroid:name="android.permission.CHANGE_CONFIGURATION"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <!--Thismeta-datatagisrequiredtouseGooglePlayServices.--> <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> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity><!--IncludetheAdActivityconfigChangesandtheme.--> <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="获取Configuration信息" android:textSize="25sp" android:layout_marginTop="80dip" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试ConfigurationChange" android:textSize="25sp" android:layout_centerInParent="true" android:id="@+id/mytext"/> </RelativeLayout>
MainActivity.java
packagecom.example.yanlei.yl7; importandroid.content.res.Configuration; importandroid.os.Bundle; importandroid.support.v7.app.AppCompatActivity; importandroid.view.Menu; importandroid.view.MenuItem; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.TextView; importandroid.widget.Toast; publicclassMainActivityextendsAppCompatActivity{ privateButtonmButton; privateTextViewpTextView; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pTextView=(TextView)this.findViewById(R.id.mytext); System.out.println("--->onCreate()"); init(); } privatevoidinit(){ mButton=(Button)findViewById(R.id.button); mButton.setOnClickListener(newClickListenerImpl()); } privateclassClickListenerImplimplementsOnClickListener{ @Override publicvoidonClick(Viewv){ getConfigurationInfo(); } } privatevoidgetConfigurationInfo(){ Configurationconfiguration=getResources().getConfiguration(); //获取屏幕方向 intl=configuration.ORIENTATION_LANDSCAPE; intp=configuration.ORIENTATION_PORTRAIT; if(configuration.orientation==l){ pTextView.setText("现在是横屏===="); System.out.println("现在是横屏"); } if(configuration.orientation==p){ pTextView.setText("现在是竖屏==="); System.out.println("现在是竖屏"); } } @Override publicvoidonConfigurationChanged(ConfigurationnewConfig){ super.onConfigurationChanged(newConfig); //newConfig.orientation获得当前屏幕状态是横向或者竖向 //Configuration.ORIENTATION_PORTRAIT表示竖向 //Configuration.ORIENTATION_LANDSCAPE表示横屏 if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){ pTextView.setText("现在是竖屏"); Toast.makeText(MainActivity.this,"现在是竖屏",Toast.LENGTH_SHORT).show(); } if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){ pTextView.setText("现在是横屏"); Toast.makeText(MainActivity.this,"现在是横屏",Toast.LENGTH_SHORT).show(); } } @Override protectedvoidonSaveInstanceState(BundleoutState){ super.onSaveInstanceState(outState); outState.putString("name","zxx"); outState.putInt("id",9527); System.out.println("--->onSaveInstanceState()"); } @Override protectedvoidonRestoreInstanceState(BundlesavedInstanceState){ super.onRestoreInstanceState(savedInstanceState); Stringname=savedInstanceState.getString("name"); intid=savedInstanceState.getInt("id"); System.out.println("--->onRestoreInstanceState()"); System.out.println("名字="+name+",编号="+id); } @Override publicbooleanonCreateOptionsMenu(Menumenu){ //Inflatethemenu;thisaddsitemstotheactionbarifitispresent. getMenuInflater().inflate(R.menu.menu_main,menu); returntrue; } @Override publicbooleanonOptionsItemSelected(MenuItemitem){ //Handleactionbaritemclickshere.Theactionbarwill //automaticallyhandleclicksontheHome/Upbutton,solong //asyouspecifyaparentactivityinAndroidManifest.xml. intid=item.getItemId(); if(id==R.id.action_settings){ returntrue; } returnsuper.onOptionsItemSelected(item); } }
以上内容给大家介绍了Android横竖屏切换小结的相关知识,希望对大家有所帮助!