本示例演示如何通過Activity瞭的setRequestedOrientation()方法來設定Activity的顯示方向。
本示例在Eclipse上編譯測試。
1. 定義清單文件(AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ScreenOrientation"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="9" />
</manifest>
2. 定義字符串資源(strings.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ScreenOrientation!</string>
<string name="app_name">ScreenOrientation</string>
<string name="screen_orientation_summary">Demonstrates the available screen
orientation modes. Often you want to set the desired mode in your manifest
instead of programmatically.</string>
<string name="screen_orientation">Screen Orientation</string>
</resources>
3. 定義數組資源(arrays.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!– 下列控件應用的數組資源 –>
<string-array name="screen_orientations">
<item>UNSPECIFIED</item>
<item>LANDSCAPE</item>
<item>PORTRAIT</item>
<item>USER</item>
<item>BEHIND</item>
<item>SENSOR</item>
<item>NOSENSOR</item>
<item>SENSOR_LANDSCAPE</item>
<item>SENSOR_PORTRAIT</item>
<item>REVERSE_LANDSCAPE</item>
<item>REVERSE_PORTRAIT</item>
<item>FULL_SENSOR</item>
</string-array>
</resources>
4. 定義佈局資源(screen_orientation.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
android:text="@string/screen_orientation_summary"/>
<Spinner android:id="@+id/orientation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/screen_orientation">
</Spinner>
</LinearLayout>
5. 創建Activity類(ScreenOrientation.java)
package my.android.test;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
publicclass ScreenOrientation extends Activity {
//Activity中的調整列表。
Spinner mOrientation;
/**
* 設定Activity主窗口的方向,數組中的方向會設定給R.attr類中的screenOrientation屬性,
* screenOrientation的屬性值必須是以下常量值。
* ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
* 不指定方向,讓系統決定Activity的最佳方向。
* ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
* 希望Activity在橫向屏上顯示,也就是說橫向的寬度要大於縱向的高度,並且忽略方向傳感器的影響。
* ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
* 希望Activity在縱向屏上顯示,也就是說縱向的高度要大於橫向的寬度,並且忽略方向傳感器的影響。
* ActivityInfo.SCREEN_ORIENTATION_USER:
* 使用用戶設備的當前首選方向。
* ActivityInfo.SCREEN_ORIENTATION_BEHIND:
* 始終保持與屏幕一致的方向,不管這個Activity在前臺還是後臺。
* ActivityInfo.SCREEN_ORIENTATION_SENSOR:
* Activity的方向由物理方向傳感器來決定,按照用戶旋轉設備的方向來顯示。
* ActivityInfo.SCREEN_ORIENTATION_NOSENSOR:
* 始終忽略方向傳感器的判斷,當用戶旋轉設備時,顯示不跟著旋轉。
* ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
* 希望Activity在橫向屏幕上顯示,但是可以根據方向傳感器指示的方向來進行改變。
* ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
* 希望Activity在縱向屏幕上顯示,但是可以根據方向傳感器指示的方向來進行改變。
* ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
* 希望Activity在橫向屏幕上顯示,但與正常的橫向屏幕方向相反。
* ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
* 希望Activity在縱向屏幕上顯示,但與正常的縱向屏幕方向相反
* ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR:
* Activity的方向由方向傳感器來決定,顯示會根據用戶設備的移動情況來旋轉。
*/
finalstaticintmOrientationValues[] = newint[]{
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED,
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
ActivityInfo.SCREEN_ORIENTATION_USER,
ActivityInfo.SCREEN_ORIENTATION_BEHIND,
ActivityInfo.SCREEN_ORIENTATION_SENSOR,
ActivityInfo.SCREEN_ORIENTATION_NOSENSOR,
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT,
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE,
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR,
};
/** 首次創建本Activity時,調用這個方法 */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//填充佈局
setContentView(R.layout.screen_orientation);
//查找下拉控件
mOrientation = (Spinner)findViewById(R.id.orientation);
//引用數組資源,創建字符數組適配器
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.screen_orientations,android.R.layout.simple_spinner_item);
//把數組適配器與下拉控件關聯
mOrientation.setAdapter(adapter);
//給下拉控件設置時間監聽器
mOrientation.setOnItemSelectedListener(
/**
* 列表項目選擇監聽器
* onItemSelected:有項目選擇時要執行這個回調。
* onNothingSelected:沒有選擇任何項目時執行這個回調。
*/
new OnItemSelectedListener(){
publicvoid onItemSelected(AdapterView<?> parent, View view, int position, long id){
//設置Activity的方向選項
setRequestedOrientation(mOrientationValues[position]);
}
publicvoid onNothingSelected(AdapterView<?> parent){
//不特定指定方向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
});
}
}
摘自 FireOfStar的專欄