Android onConfigurationChanged 旋转问题

发布于 2024-11-23 20:34:10 字数 564 浏览 5 评论 0原文

我在清单中将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温暖的光 2024-11-30 20:34:10

除非绝对必要,否则不应使用 configChanges 属性(引入此属性是为了启用某些性能敏感的场景)。请仔细阅读处理运行时[配置]更改

此技术[使用configChanges]应被视为最后的手段,不建议大多数应用程序使用。


警告您后,这里解释了为什么旋转设备时看不到变化。

  • 如果您不在 Activity 上使用 configChanges="orientation",您的 Activity 将被销毁并使用新方向的资源重新创建。在这种情况下,onCreate() 中的代码将自动从正确的资源(*-land*-port)中选取值。

  • 如果您确实使用configChanges="orientation",您基本上是在告诉 Android 不要自动应用更改,并且您将在 onConfigurationChanged() 中自行执行此操作。

假设您确实需要保留 configChanges,您可以执行以下操作来解决字体大小问题:

@Override
public void onConfigurationChanged(Configuration newConfig)
{       
     super.onConfigurationChanged(newConfig);

     int fontSize = getResources().get(R.id.font_size);     
     mTextView.setTextSize(fontSize);
}

但是,在大多数情况下,您确实不应该使用 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.

This technique [using configChanges] should be considered a last resort and is not recommended for most applications.


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 in onCreate() 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 in onConfigurationChanged().

Assuming you really need to keep configChanges, here is what you can do to fix issue with font size:

@Override
public void onConfigurationChanged(Configuration newConfig)
{       
     super.onConfigurationChanged(newConfig);

     int fontSize = getResources().get(R.id.font_size);     
     mTextView.setTextSize(fontSize);
}

But again, you really should not use configChanges in most cases.

ゝ杯具 2024-11-30 20:34:10

如果您在 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.

鹤仙姿 2024-11-30 20:34:10

您实际上可以为两种布局类型指定两个不同的布局文件,并根据需要配置每个文件。

一些文档:应用程序资源

如果您只想进行外观设计更改基于方向,这可能是首选方法

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文