在Android中要让一个程序的界面始终保持一个方向,不随手机方向转动而变化的办法: 只要在AndroidManifest.xml里面配置一下就可以了。
在AndroidManifest.xml的activity(需要禁止转向的activity)配置中加入 android:screenOrientation=”landscape”属性即可(landscape是横向,portrait是纵向)。例如:
Java 代码
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>
<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)这个钩子方法。例如:
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. }
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>
来源:oschina
链接:https://my.oschina.net/u/573470/blog/82546