在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>