根据方向提供不同的_功能_(不仅仅是布局)?
我有一个监控应用程序,它以动画形式显示一组四个参数(例如,它使用温度计图形显示温度)。我还有一个屏幕,将所有四个参数值绘制在图表上。
我的要求是:
- 每当设备处于横向模式时,都应该显示图表。
- 每当设备返回纵向模式时,都应显示较早的动画屏幕。
我想知道是否可以根据方向提供不同的功能。我看到 2 个选项
- Override
Activity.onConfigurationChanged()
- 但然后呢?我现在可以执行setContentView()
吗?或者,我可以启动另一个活动(在单独的仅限横向模式的活动中显示图表的活动)吗? - 重写
Application.onConfigurationChanged()
方法。但坦率地说,我不知道如何继续下去。
I have a monitoring app that displays a set of four parameters as animations (for example, it displays the temperature using a thermometer graphic). I also have one screen which plots all the four parameter values on a chart.
My requirement is this:
- Whenever the device is in Landscape mode, the chart should be displayed.
- Whenever the device is back in portrait mode, the earlier animation screen should be displayed.
I'm wondering if it is possible to provide different functionality based on the orientation. I see 2 options
- Override
Activity.onConfigurationChanged()
- but then what? Can i do asetContentView()
at this point? Or, can i launch a different Activity (one which displays the Chart in a separate, lanscape-mode-only Activity)? - Override
Application.onConfigurationChanged()
method. But I frankly haven't a clue as to how to proceed with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以在
Activity.onConfigurationChanged()
内调用setContentView()
。您可以检查方向并相应地执行一些任务,即根据方向运行不同的代码。正常行为是当方向改变时,系统会终止 Activity 并创建一个新的 Activity。您可以在活动清单中使用
android:configChanges="orientation"
来防止这种情况发生。在这种情况下,您需要重写onConfigurationChanged(..)
方法,该方法将在发生更改时调用。请参阅处理运行时更改。Yes you can call
setContentView()
insideActivity.onConfigurationChanged()
. You can check orientation and accordingly perform some tasks, i.e. run different code based on orientation.Normal behaviour is that when orientation changes, system kills Activity and creates a new one. You can prevent this with
android:configChanges="orientation"
in you activity manifest. In this case you need to override theonConfigurationChanged(..)
method, which will be called when change happens. See handling runtime changes.