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)下面讲讲方向属性值的问题
### Dialog 主题 Activity 中设置 `android:configChanges="orientation|keyboardHidden"` 可能导致的问题 在使用 `Dialog` 主题的 Activity 时,若配置了 `android:configChanges="orientation|keyboardHidden"` 属性,可能会引发一系列与屏幕方向变化和 UI 状态管理相关的问题。 #### 1. **布局适配问题** 当设备发生横竖屏切换时,系统会重新创建 Activity,而是调用 `onConfigurationChanged()` 法。这意味着当前的 UI 布局会自动切换到针对特定方向的资源目录(如 `layout-land` 或 `layout-port`)中的布局文件。对于 Dialog 样式的 Activity 来说,这种行为可能导致界面显示完整或错位,尤其是在设计上依赖于特定方向布局的情况下 [^3]。 ```xml <activity android:name=".DialogActivity" android:theme="@style/DialogActivityStyle" android:configChanges="orientation|keyboardHidden" /> ``` #### 2. **Dialog 显示异常** 由于 Dialog 样式的 Activity 实际上是一个独立的窗口,其生命周期与普通 Activity 相同。如果在屏幕旋转时重新创建 Activity,但 Dialog 已经显示并且未被正确更新或关闭,则可能会出现窗体泄漏(Window Leak)问题。这种情况通常发生在没有正确处理 Dialog 的销毁与重建逻辑时 [^2]。 #### 3. **UI 元素状态丢失** 即使 Activity 会因屏幕方向变化而重启,某些 UI 元素的状态(如输入框内容、按钮选中状态等)仍可能因未正确保存和恢复而导致数据丢失或显示错误。虽然可以通过 `onSaveInstanceState()` 和 `onRestoreInstanceState()` 来缓解这一问题,但在 Dialog 样式下,这类操作的复杂度会增加 [^5]。 #### 4. **性能优化挑战** 尽管通过 `configChanges` 避免了完整的生命周期重启,从而提升了响应速度,但对于 Dialog 样式的 Activity 来说,手动处理屏幕方向变化带来的 UI 更新任务反而可能引入额外的性能开销。例如,需要编写额外代码来检测方向变化并动态调整布局参数,这在某些情况下甚至可能导致更高的 CPU 使用率 [^5]。 #### 5. **用户体验一致性问题** 用户期望 Dialog 类型的界面在方向下保持一致的行为和外观。然而,如果仅通过 `configChanges` 控制屏幕方向变化而进行相应的 UI 调整,可能会导致体验上的割裂感。特别是在大屏设备或多窗口模式下,这种差异更加明显 [^1]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值