Screen rotation
I was writing very simple application in Android with screen rotation. I woud like "repair" this code, because screen don't rotate.
I add code in manifest:
android:configChanges="orientation|keyboardHidden">
Rest of code:
package com.example;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class ScreenOrientationActivity extends Activity {
public void onSavedInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
}
public void onConfigurationChanged(Configuration config){
super.onConfigurationChanged(config);
if(config.orientation==Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.landscape);
}
if(config.orientation==Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.main);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
}
}
**EDIT:**
My full manifest.xml- his not have line android:screenOrientation=portrait:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0"
android:configChanges="orientation|keyboardHidden">
<uses-sdk android:min开发者_开发知识库SdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ScreenOrientationActivity"
android:label="@string/app_name"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
had you added orientation=portrait in your manifest's activity tag?
You don't need to do it manually in onConfigChanges... Just add a library called: layout-land in the same level as layout and put there the layout files for landscape mode. Android will do it automatically for you.
Read here: http://developer.android.com/guide/topics/resources/providing-resources.html
精彩评论