android中禁止横竖屏切换

在Android中要让一个程序的界面始终保持一个方向,不随手机方向转动而变化的办法: 只要在AndroidManifest.xml里面配置一下就能够了。android

在AndroidManifest.xml的activity(须要禁止转向的activity)配置中加入 android:screenOrientation=”landscape”属性便可(landscape是横向,portrait是纵向)。例如:
Java 代码app

1. <application android:persistent="true" 
2.       android:label="@string/home_title" 
3.       android:icon="@drawable/ic_launcher_home"> 
4.  
5.     <activity android:name="Home" 
6.             android:theme="@style/Theme" 
7.             android:launchMode="singleInstance" 
8.             android:stateNotNeeded="true" 
9.             android:screenOrientation="portrait"> 
10.         <intent-filter> 
11.             <action android:name="android.intent.action.MAIN" /> 
12.             <category android:name="android.intent.category.HOME"/> 
13.             <category android:name="android.intent.category.DEFAULT" /> 
14.         </intent-filter> 
15.     </activity>  ide

<application android:persistent="true"
android:label="@string/home_title"
android:icon="@drawable/ic_launcher_home">函数

<activity android:name="Home"
android:theme="@style/Theme"
android:launchMode="singleInstance"
android:stateNotNeeded="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>布局


另外,android中每次屏幕方向切换时都会重启Activity,因此应该在Activity销毁前保存当前活动的状态,在Activity 再次Create的时候载入配置,那样,进行中的游戏就不会自动重启了!测试

要避免在转屏时重启activity,能够经过在androidmanifest.xml文件中从新定义方向(给每一个activity加上 android:configChanges=”keyboardHidden|orientation”属性),并根据Activity的重写 onConfigurationChanged(Configuration newConfig)方法来控制,这样在转屏时就不会重启activity了,而是会去调用 onConfigurationChanged(Configuration newConfig)这个钩子方法。例如:this

Java 代码命令行

1. if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){ 
2.   //横向  
3.   setContentView(R.layout.file_list_landscape);  
4. }else{ 
5.   //竖向  
6.    setContentView(R.layout.file_list);  
7. }  xml

if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//横向
setContentView(R.layout.file_list_landscape);
}else{
//竖向
setContentView(R.layout.file_list);
}游戏


在模拟器中,要使程序转屏可使用快捷键F12或Ctrl+F11来切换。固然在用命令行启动模拟器时能够直接使用参数emulator.exe -skin HVGA-L来启动横屏的程序。

 

android 禁止横竖屏切换时调用onCreate函数
2011-08-04 18:04 385人阅读 评论(0) 收藏 举报
在横竖屏切换的时候会从新调用onCreate方法,可使用如下方法禁止切换的过程当中调用onCreate方法,若是你须要横屏和竖屏使用不一样的布局文件,可能这种方式是不行的,通过我本身写代码测试,若是当前是竖屏,通过切换后它并不会使用横屏的布局文件,而是将竖屏的布局文件使用在横屏状态下,不知道是我写的不合适仍是原本就这样,但愿和你们讨论这个问题。                                                                                                        

禁止调用onCreate方法:在Mainifest.xml的Activity元素中加入android:configChanges="orientation|keyboardHidden"属性                                           

我写的测试程序:                                                                                                                                                                                               

view plaincopy to clipboardprint?package lzu.LandAndPortraintTest; 
 
import android.app.Activity; 
import android.content.pm.ActivityInfo; 
import android.content.res.Configuration; 
import android.os.Bundle; 
 
public class LandAndPortraintTest extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        printSentence(); 
    } 
    public void printSentence(){ 
        System.out.println("printSentence"); 
    } 
    public void printPortraintSentence(){ 
        System.out.println("printSentence Portraint"); 
    } 
    public void printLandscapeSentence(){ 
        System.out.println("printSentence Landscape"); 
    } 
    public void onConfigurationChanged(Configuration newConfig) 
    {  
        super.onConfigurationChanged(newConfig);  
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) 
        { 
            printLandscapeSentence(); 
        } 
        else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) 
        { 
            printPortraintSentence(); 
        } 
    } 

package lzu.LandAndPortraintTest;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;

public class LandAndPortraintTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        printSentence();
    }
    public void printSentence(){
     System.out.println("printSentence");
    }
    public void printPortraintSentence(){
     System.out.println("printSentence Portraint");
    }
    public void printLandscapeSentence(){
     System.out.println("printSentence Landscape");
    }
    public void onConfigurationChanged(Configuration newConfig)
    {
     super.onConfigurationChanged(newConfig);
     if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
     {
      printLandscapeSentence();
     }
     else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
     {
      printPortraintSentence();
     }
    }
}layout-land下main.xml文件:view plaincopy to clipboardprint?<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/hello" 
    /> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>layout-port下main.xml文件:view plaincopy to clipboardprint?<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/app_name" 
    /> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     > <TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/app_name"     /> </LinearLayout> values下strings.xml文件:view plaincopy to clipboardprint?<?xml version="1.0" encoding="utf-8"?>  <resources>      <string name="hello">Hello World, LandAndPortraintTest!</string>      <string name="app_name">LandAndPortraintTest</string>  </resources>  <?xml version="1.0" encoding="utf-8"?> <resources>     <string name="hello">Hello World, LandAndPortraintTest!</string>     <string name="app_name">LandAndPortraintTest</string> </resources>

相关文章
相关标签/搜索