禁止Android 横屏竖屏切换 .

fmms 13年前
     <p style="text-align:left;line-height:26px;font-family:Arial;color:#333333;font-size:14px;">在Android中要让一个程序的界面始终保持一个方向,不随手机方向转动而变化的办法: 只要在AndroidManifest.xml里面配置一下就可以了。</p>    <p style="text-align:left;line-height:26px;font-family:Arial;color:#333333;font-size:14px;">在AndroidManifest.xml的activity(需要禁止转向的activity)配置中加入android:screenOrientation=”landscape”属性即可(landscape是横向,portrait是纵向)。</p>    <p style="text-align:left;line-height:26px;font-family:Arial;color:#333333;font-size:14px;"></p>    <p style="text-align:left;line-height:26px;font-family:Arial;color:#333333;font-size:14px;">另外,android中每次屏幕方向切换时都会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置。要避免在转屏时重启activity,可以通过在androidmanifest.xml文件中重新定义方向(给每个activity加上android:configChanges=”keyboardHidden|orientation”属性),并根据Activity的重写onConfigurationChanged(Configuration newConfig)方法来控制,这样在转屏时就不会重启activity了,而是会去调用onConfigurationChanged(Configuration newConfig)这个方法。</p>    <p style="text-align:left;line-height:26px;font-family:Arial;color:#333333;font-size:14px;">示例代码如下:</p>    <pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.liananse.ideal"     android:versionCode="1"     android:versionName="1.1" >      <uses-sdk android:minSdkVersion="10" />      <application         android:icon="@drawable/ic_launcher"         android:label="@string/app_name" >         <activity             android:label="@string/app_name"             android:name="com.liananse.WelcomeActivity"              android:screenOrientation="portrait"             android:configChanges="keyboardHidden|orientation">             <intent-filter >                 <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>         <activity              android:name="com.liananse.activity.MainActivity"             android:screenOrientation="portrait"             android:configChanges="keyboardHidden|orientation"></activity>         <activity              android:name="com.liananse.activity.MusicActivity"             android:screenOrientation="portrait"             android:configChanges="keyboardHidden|orientation"></activity>         <activity              android:name="com.liananse.activity.config.IdealAbout"             android:theme="@android:style/Theme.NoTitleBar"             android:screenOrientation="portrait"             android:configChanges="keyboardHidden|orientation"></activity>         <activity              android:name="com.liananse.activity.config.IdealSetting"             android:theme="@android:style/Theme.NoTitleBar"             android:screenOrientation="portrait"             android:configChanges="keyboardHidden|orientation"></activity>         <activity              android:name="net.youmi.android.AdActivity"          android:configChanges="keyboard|keyboardHidden|orientation"          android:screenOrientation="portrait"/>         <meta-data android:name="YOUMI_CHANNEL" android:value="0" />     </application>     <uses-permission android:name="android.permission.INTERNET"/>  <uses-permission android:name="android.permission.READ_PHONE_STATE"/>  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> </manifest></pre>