当前位置: 代码迷 >> Android >> Android控制银屏方向的改变
  详细解决方案

Android控制银屏方向的改变

热度:53   发布时间:2016-05-01 16:37:12.0
Android控制屏幕方向的改变

???? 目前大多数手机都支持 重力感应 ,随之而来的就是屏幕方向改变的问题。很多游戏都是仅横屏展示的,也有一些是仅竖屏展示的,更多的是横屏竖屏都可以的。

?

??? 对应普通开发者来说,屏幕的随意改变也会带来困扰。在Google自带的doc里可以看到

? 如果设备的配置(在 Resources.Configuration 中进行了定义)发生改变,那么所有用户界面上的东西都需要进行更新,以适应新的配置。因为 Activity 是与用户交互的最主要的机制,它包含了处理配置改变的专门支持。除非你特殊指定,否则当配置发生改变(比如屏幕方向、语言、输入设备等等的改变)时你当前的 activity 都将被销毁,这销毁是通过一个正常的 activity 生命周期过程( onFreeze(Bundle) , onPause() , onStop() , onDestroy() )进行的。如果 activity 之前正在前景画面,当这个实例的 onDestroy() 调用完成后将会启动这个 activity 的一个新的实例,并将前面那个实例中 onFreeze(Bundle) 所保存的内容传递给新的实例。因为任何的应用资源(包括 layout 文件)都有可能由于任何配置值而改变。因此处理配置改变的唯一安全的方法就是重新获取所有的资源,包括 layout 、绘图资源(原文 drawables )、字符串资源。由于 activity 已经如何保存自己的状态并从这些状态中重建自身,所以 activity 重新启动自身来获得新的配置将是一个非常便利的途径。在一些特殊的情况中,你可能希望当一种或者多种配置改变时避免重新启动你的 activity 。你可以通过在manifest中设置 android:configChanges 属性来实现这点。你可以在这里声明 activity 可以处理的任何配置改变,当这些配置改变时不会重新启动 activity ,而会调用 activity onConfigurationChanged(Resources.Configuration) 方法。如果改变的配置中包含了你所无法处理的配置(在 android:configChanges 并未声明),你的 activity 仍然要被重新启动,而 onConfigurationChanged(Resources.Configuration) 将不会被调用。

?

?? 在屏幕方向改变的时候,如果没有处理,程序会自动重启。对应一些需要保存用户数据的应用中,必须处理这种情况。

?

1>在AndroidManifest.xml中设置Activity的android:configChanges 属性。如:

?

<activity android:name=".AndroidLight"                  android:label="@string/app_name"                  android:configChanges="orientation|keyboardHidden">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>

?

?

?

??

这样就指定了屏幕方向改变和键盘隐藏时通知程序。

?

2>在程序中可以添加处理事件

?

@Override	public void onConfigurationChanged(Configuration newConfig) {	  super.onConfigurationChanged(newConfig);	  Log.d(TAG," == onConfigurationChanged");	  processLayout();//自定义函数处理配置改变事件	}

?

?

3>也可以在AndroidManifest.xml中设置Activity的android:screenOrientation属性。如

?

<activity android:name=".AndroidLight"                  android:label="@string/app_name"                  android:screenOrientation="portrait">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter></activity>

?

??

这样指定屏幕方向为竖屏。屏幕就不会自动旋转了。

?

?

4>?android:configChanges 可选属性值?

?

?

Constant

Value

Description

mcc

0x0001

The IMSI MCC has changed, that is a SIM has been detected and updated the Mobile Country Code.

mnc

0x0002

The IMSI MNC has changed, that is a SIM has been detected and updated the Mobile Network Code.

locale

0x0004

The locale has changed, that is the user has selected a new language that text should be displayed in.

touchscreen

0x0008

The touchscreen has changed. Should never normally happen.

keyboard

0x0010

The keyboard type has changed, for example the user has plugged in an external keyboard.

keyboardHidden

0x0020

The keyboard or navigation accessibility has changed, for example the user has slid the keyboard out to expose it. Note that despite its name, this applied to any accessibility: keyboard or navigation.

navigation

0x0040

The navigation type has changed. Should never normally happen.

orientation

0x0080

The screen orientation has changed, that is the user has rotated the device.

screenLayout

0x0100

The screen layout has changed. This might be caused by a different display being activated.

uiMode

0x0200

The global user interface mode has changed. For example, going in or out of car mode, night mode changing, etc.

fontScale

0x40000000

The font scaling factor has changed, that is the user has selected a new global font size.

?

?

?

5>android:screenOrientation的可选属性值

Constant

Value

Description

unspecified

-1

No preference specified: let the system decide the best orientation. This will either be the orientation selected by the activity below, or the user's preferred orientation if this activity is the bottom of a task. If the user explicitly turned off sensor based orientation through settings sensor based device rotation will be ignored. If not by default sensor based orientation will be taken into account and the orientation will changed based on how the user rotates the device

landscape

0

Would like to have the screen in a landscape orientation: that is, with the display wider than it is tall, ignoring sensor data.

portrait

1

Would like to have the screen in a portrait orientation: that is, with the display taller than it is wide, ignoring sensor data.

user

2

Use the user's current preferred orientation of the handset.

behind

3

Keep the screen in the same orientation as whatever is behind this activity.

sensor

4

Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device.

nosensor

5

Always ignore orientation determined by orientation sensor: the display will not rotate when the user moves the device.

sensorLandscape

6

Would like to have the screen in landscape orientation, but can use the sensor to change which direction the screen is facing.

sensorPortait

7

Would like to have the screen in portrait orientation, but can use the sensor to change which direction the screen is facing.

reverseLandscape

8

Would like to have the screen in landscape orientation, turned in the opposite direction from normal landscape.

reversePortait

9

Would like to have the screen in portrait orientation, turned in the opposite direction from normal portrait.

fullSensor

10

Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device. This allows any of the 4 possible rotations, regardless of what the device will normally do (for example some devices won't normally use 180 degree rotation).

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

1 楼 sunnyting 2012-06-15  
蛮靠谱!解决问题哈!
  相关解决方案