Android中方向问题(转变方向)以及 configChanges 配置不管用问题

本文详细介绍了在Android中设置屏幕方向的两种方式,包括在AndroidManifest.xml中配置activity的android:orientation属性和在Activity的onCreate()方法中使用setRequestedOrientation()。讨论了各种方向属性值的含义,如landscape、portrait、sensor等,并解释了当配置configChanges时,如何处理屏幕旋转生命周期问题,避免Activity重建。此外,还提到了在onConfigurationChanged()方法中监听和处理配置变化的方法。

1. 有两种设置屏幕方向的方式:

1)在AndroidManifest.xml 中配置activity时增加 android:orientation属性即可,如:

     <activity android:name=".TestActivity" android:screenOrientation="xxx">

    xxx代表的值可选项如下:

  

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.
sensorPortrait 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.
reversePortrait 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).

2)在Activity 的 onCreate()方法中,注意在setContentView(R.layout.xx)之后,调用      setRequestedOrientation(int requestOrientation)

这个参数值在类 ActivityInfo 中定义了很多常量:


int SCREEN_ORIENTATION_BEHIND
int SCREEN_ORIENTATION_FULL_SENSOR
int SCREEN_ORIENTATION_LANDSCAPE
int SCREEN_ORIENTATION_NOSENSOR Constant corresponding to nosensor in the screenOrientation attribute.
int SCREEN_ORIENTATION_PORTRAIT Constant corresponding to portrait in the screenOrientation attribute.
int SCREEN_ORIENTATION_REVERSE_LANDSCAPE Constant corresponding to reverseLandscape in the screenOrientation attribute.
int SCREEN_ORIENTATION_REVERSE_PORTRAIT Constant corresponding to reversePortrait in the screenOrientation attribute.
int SCREEN_ORIENTATION_SENSOR Constant corresponding to sensor in the screenOrientation attribute.
int SCREEN_ORIENTATION_SENSOR_LANDSCAPE Constant corresponding to sensorLandscape in the screenOrientation attribute.
int SCREEN_ORIENTATION_SENSOR_PORTRAIT Constant corresponding to sensorPortrait in the screenOrientation attribute.
int SCREEN_ORIENTATION_UNSPECIFIED Constant corresponding to unspecified in the screenOrientation attribute.
int SCREEN_ORIENTATION_USER Constant corresponding to user in the screenOrientation attribute.

3)下面讲讲方向属性值的问题

 behind :  加入Activity A 点击 按钮进入 Activity B, 而 Activity B设置的 orientation值为 behind,则B的屏幕方向和A相同。

                 使用Activity堆栈中与该Activity之下的那个Activity的相同的方向

 fullSensor : sensor中文是传感器的意思,所以,这个就是说方向有手机的重力反应决定,如果你竖着拿手机,屏幕方向就是竖着,

 sensor : 这个和上面的fullSensor 差不多,唯一的差别是 fullSensor 支持多个方向的转换(四个方向都可以)

 landscape : 是横屏属性

 portrait : 竖屏属性值,默认就是这个

sensorLandscape: 横屏也有左右之分,是吧,当你是横屏时,反转180度还可以反转为另外一种横屏

sensorPortrait: 类似上面的横屏反转

-----------------------------------------------------------------------

下面两个实验没有效果,应该是对于某些android系统传感器设置的吧:

 reverseLandscape:如果是横屏,则转换为与横屏相反

 reversePortrait : 如果是竖屏,则调整为与竖屏相反

 

user: 使用用户当前首选的方向

unspecified : 默认值,由系统选择方向


2. 屏幕反转的生命周期的问题以及onConfigChanges的使用

1)屏幕反转的生命周期 ,如果你没有设置 onConfigChanges 属性,则一般只有你的屏幕反转都会重新创建(onCreate)


2)如果不想Activity重建,则在 AndroidManifest.xml  的activity中配置:

      <activity android:name=""

android:screenOrientation="" 

        android:configChanges="orientation|screenSize|layoutDirection" />

     在这里大家要注意:::::::

     只配置为 android:configChanges="orientation" 是不行的,必须喝 screenSize 一起使用,layoutDirection无光紧要

     此时就不会重建 Activity了,当然会回调Activity里面的一个方法: onConfigurationChanged(Configuration config)

     在这里你可以监听了,Activity的什么改变了,比如方向,比如弹出了键盘还是隐藏了键盘(这里要在android:configChanges="keyboard|keyboardHidden“)

     至此,对方向问题也就总结的差不多了,最后,大家有时间自己去看看 Configuration 类,了解下怎么判断Activity的什么东西改变了。 




3)下面讲讲方向属性值的问题
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值