Android onConfigurationChanged 旋转问题
我在清单中将 android:configChanges="orientation|keyboard"
赋予了我的 Activity,当我旋转设备时,onConfigurationChanged
始终会被调用。没问题。
我想要的是当设备旋转时,小部件的字体大小会改变。
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/my_font_size" />
与上面一样,textSize
属性引用资源中的值 定义 my_font_size
的 xml 文件位于 value-land、values-port 文件夹中。
好的。我准备好了。构建它,运行它,然后旋转设备,没有任何变化。 onConfigurationChanged() 中的 requestLayout() 不起作用。 有什么建议吗?
谢谢。
I gave android:configChanges="orientation|keyboard"
to my activity in manifest and when I rotate my device, onConfigurationChanged
is always called. No problem with it.
What I want is when device is rotated, font size of widget become changed.
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/my_font_size" />
Like above, textSize
attribute refer the value in resource
and the xml file defining my_font_size
is in values-land, values-port folders.
Ok. I'm ready. Build it, run it, and rotate the device, and there is no change.
requestLayout() in onConfigurationChanged() doesn't work.
Any advise about it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非绝对必要,否则不应使用 configChanges 属性(引入此属性是为了启用某些性能敏感的场景)。请仔细阅读处理运行时[配置]更改。
警告您后,这里解释了为什么旋转设备时看不到变化。
如果您不在 Activity 上使用
configChanges="orientation"
,您的 Activity 将被销毁并使用新方向的资源重新创建。在这种情况下,onCreate()
中的代码将自动从正确的资源(*-land
或*-port
)中选取值。如果您确实使用
configChanges="orientation"
,您基本上是在告诉 Android 不要自动应用更改,并且您将在onConfigurationChanged()
中自行执行此操作。假设您确实需要保留 configChanges,您可以执行以下操作来解决字体大小问题:
但是,在大多数情况下,您确实不应该使用 configChanges。
You shouldn't use
configChanges
attribute unless its absolutely necessary (this attribute is introduced to enable some performance-sensitive scenarios). Please carefully read Handling Runtime [Congiuration] Changes.Having warned you, here is explanation why you do not see changes when rotating the device.
If you do not use
configChanges="orientation"
on your activity, you activity is destroyed and recreated with resources for new orientation. In this case your code inonCreate()
will automatically pick values from correct resources (*-land
or*-port
).If you do use
configChanges="orientation"
you basically saying to Android to not apply changes automatically, and that you will do this by yourself inonConfigurationChanged()
.Assuming you really need to keep
configChanges
, here is what you can do to fix issue with font size:But again, you really should not use
configChanges
in most cases.如果您在 Activity 中指定
android:configChanges="orientation|keyboard"
,那么 Android 将不会为您处理此更改(这意味着它不会使用正确的资源文件重新创建您的 Activity)。这就是为什么你看不到字体大小的变化。如果您希望 Android 处理此问题,请不要将 configChanges 添加到清单中的 Activity 中。If you specify
android:configChanges="orientation|keyboard"
in your activity then Android won't handle this changes for you (meaning that it won't recreate your activity using the proper resource files). That's why you don't get the change in the font size. If you want Android to handle this, then don't add the configChanges to your Activity in the Manifest.您实际上可以为两种布局类型指定两个不同的布局文件,并根据需要配置每个文件。
一些文档:应用程序资源
如果您只想进行外观设计更改基于方向,这可能是首选方法
You can actually specify two different layout files for the two layout types, and configure each with what you want.
Some documentation: Application Resources
If all you want is to make cosmetic design changes based on orientation that is probably the preferred method