最終更新:
moonlight_aska 2015年12月01日(火) 22:51:51履歴
画面の向きは, 縦/横方向に固定あるいはセンサによる自動切り換えに設定できる.
設定する方法はいくつかある.
動作例:
1) 縦方向

2) 横方向

設定する方法はいくつかある.
- 画面の向きを設定する属性
属性値 | 値 | 説明 | API Level |
unspecified | SCREEN_ORIENTATION_UNSPECIFIED | 端末の設定により動作が異なる(デフォルト) | 1 |
landscape | SCREEN_ORIENTATION_LANDSCAPE | 横固定(ノーマル) | 1 |
portrait | SCREEN_ORIENTATION_PORTRAIT | 縦固定(ノーマル) | 1 |
user | SCREEN_ORIENTATION_USER | 現在のレイアウトに従う | 1 |
behind | SCREEN_ORIENTATION_BEHIND | 親のアクティビティに従う | 1 |
sensor | SCREEN_ORIENTATION_SENSOR | センサ状態に従う. ユーザの端末の持ち方に依存 | 1 |
nosensor | SCREEN_ORIENTATION_NOSENSOR | センダ状態に従わない以外はunspecifiedと同じ | 1 |
sensorLandscape | SCREEN_ORIENTATION_SENSOR_LANDSCAPE | 横方向(ノーマル/リバース). センサ状態に従う | 9 |
sensorPortrait | EEN_ORIENTATION_SENSOR_PORTRAIT | 縦方向(ノーマル/リバース). センサ状態に従う | 9 |
reverseLandscape | SCREEN_ORIENTATION_REVERSE_LANDSCAPE | 横固定(リバース) | 9 |
reversePortrait | SCREEN_ORIENTATION_REVERSE_PORTRAIT | 縦固定(リバース) | 9 |
fullSensor | SCREEN_ORIENTATION_FULL_SENSOR | センサ状態に従う | 9 |
- Orientation.java
- Activity#setRequestedOrientationメソッドで, 画面の向きを設定する.
package com.moonlight_aska.android.orientation;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
public class Orientation extends Activity implements View.OnClickListener {
private Button btn01 = null;
private Button btn02 = null;
private Button btn03 = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn01 = (Button)findViewById(R.id.btn01_id);
btn02 = (Button)findViewById(R.id.btn02_id);
btn03 = (Button)findViewById(R.id.btn03_id);
btn01.setOnClickListener(this);
btn02.setOnClickListener(this);
btn03.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == btn01) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(v == btn02) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if(v == btn03) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}
}
- AndroidManifest.xml
- android:screenOrientationに画面の向きを設定する.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moonlight_aska.android.orientation"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Orientation"
android:label="@string/app_name" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
動作例:
1) 縦方向

2) 横方向

コメントをかく