2010年10月28日星期四

通過Java程序碼, 運行時設置屏幕方向 - setRequestedOrientation()

上文"通過XML設置屏幕方向(android:screenOrientation)"中, 在AndroidManifest.xml文件設定屏幕方向. 這種方法有一個缺點:不能在程序運行時改變屏幕方向. 這篇文章用另一種方法 - 使用setRequestedOrientation()方法, 在程序運行時設置屏幕方向.

通過Java程序碼, 運行時設置屏幕方向 - setRequestedOrientation()

main.xml
<?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"
/>
<Button
android:id="@+id/setrequestedorientation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Requested Orientation"
/>
</LinearLayout>


SetRequestedOrientationActivity.java 負責選擇屏幕方向.
package com.AndroidRequestedOrientation;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SetRequestedOrientationActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, OrientationString));
getListView().setTextFilterEnabled(true);
}

static final String[] OrientationString = new String[] {
"SCREEN_ORIENTATION_UNSPECIFIED",
"SCREEN_ORIENTATION_LANDSCAPE",
"SCREEN_ORIENTATION_PORTRAIT",
"SCREEN_ORIENTATION_USER",
"SCREEN_ORIENTATION_BEHIND",
"SCREEN_ORIENTATION_SENSOR",
"SCREEN_ORIENTATION_NOSENSOR"
};

static final int[] OrientationSetting = new int[] {
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
};

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);

Intent intent = new Intent();
Bundle bundle = new Bundle();

bundle.putInt("setting", OrientationSetting[position]);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}

}


主代碼 - AndroidRequestedOrientation.java
在onActivityResult()方法, 從SetRequestedOrientationActivity返回值, 使用setRequestedOrientation()方法, 設置屏幕方向.
package com.AndroidRequestedOrientation;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidRequestedOrientation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonSetRequestedOrientation = (Button)findViewById(R.id.setrequestedorientation);
buttonSetRequestedOrientation.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(AndroidRequestedOrientation.this,
SetRequestedOrientationActivity.class);

startActivityForResult(intent, 0);
}});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (requestCode==0)
{
if(resultCode == RESULT_OK){
int OrientationSetting = data.getIntExtra("setting",
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
setRequestedOrientation(OrientationSetting);
}
}
}
}


記得在AndroidManifest.xml文件加入SetRequestedOrientationActivity活動.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidRequestedOrientation"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidRequestedOrientation"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SetRequestedOrientationActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="4" />

</manifest>

沒有留言:

發佈留言