Android,Tabhost 的方向问题,非常奇怪
每个人。
我创建了一个 Tabhost,上面有 4 个按钮。
我在 menifest 文件中的每个 Activity 上设置 android:configChanges="orientation|keyboardHidden" 。
我还重写了 onConfigurationChanged 方法以在屏幕定向时侦听该事件。
现在出现了一个“Bug”,不知道如何解决。
当我单击 4 个按钮(例如 A、B、C 和 D)之一,并将平板电脑的方向从纵向旋转为横向时,活动正常。它的 onConfigurationChanged 方法有效。
但是当我在横向模式下单击 B 按钮时,其纵向状态仍然保持(我希望调用 B 按钮的 onConfigChanged 方法,但它没有)。总之,如果您更改了方向,然后单击另一个 Tab 按钮,则不会调用 Activity 的 onConfigurationChanged 方法。
我该如何解决这个问题?多谢。
everyone.
I have created a Tabhost and there are 4 buttons on it.
I set android:configChanges="orientation|keyboardHidden" on every Activity in the menifest file.
And I also override the onConfigurationChanged method to listen the event when the screen is oriented.
Now there is a "Bug", and I don't know how to solve it.
When I click on one of the 4 buttons(for example, A, B, C, and D), and I rotate the Tablet's orientation from portrait to landscape, the Activity is fine. Its onConfigurationChanged method works.
But When I click the B button on landscape mode, its portrait state is still kept(I want B button's onConfigChanged method to be called, but it doesn't). In a word, if you changed the orientation, and you click another Tab button, the Activity's onConfigurationChanged method won't be called.
How can I solve this problem? Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了类似问题的解决方案。
我正在使用 tabhost,希望 4 个选项卡中的 3 个能够旋转,1 个选项卡仅设置为纵向模式。以前(应用此修复之前),3 个选项卡会旋转,直到选择固定的纵向选项卡,然后它们都卡在纵向模式下。
我必须设置其他选项卡以使用用户的方向来解决此问题。即在其他 3 个选项卡的 onResume 中添加了
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
,它现在完全按照我的预期工作。现在,3 个选项卡将根据用户的设备方向旋转,1 个选项卡固定为纵向。 :)I found a solution to a similar problem.
I'm using tabhost and want 3 of 4 tabs to be able to rotate and 1 tab set to portrait mode only. Previously (before applying this fix) the 3 tabs would rotate until the fixed portrait tab was selected, then they were all stuck in portrait mode.
I had to set the other tabs to use the orientation of the USER to remedy this. i.e. added a
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
in the onResume of the other 3 tabs and it now works exactly as I had intended. 3 tabs will now rotate based on the user's orientation of the device, and 1 tab is fixed portrait. :)您无法为视图或任何类型的回调调用
onConfigChanged()
,据我所知,它只能从设备调用。除了这个评论之外我没有任何想法。You cant call
onConfigChanged()
for a view or any kind of callback, it is called only from the device, as far as I know. I have no ideas besides this comment.删除该行:
尝试从 onConfigurationChanged() 函数中 。如果这不起作用,请发布您打开的内容/单击任何选项卡时发生的情况,以便我们了解可能导致问题的原因。
但是,执行您尝试的操作的最佳方法是将可绘制对象放入资源下的正确目录中,并为两者使用相同的名称。这样,您就不需要指定用于不同配置的可绘制对象,而是让 Android 操作系统为您执行此操作。将一个放在
drawable
目录中,另一个放在drawable-land
或drawable-port
中,具体取决于默认方向。您可以在此处阅读有关如何提供资源并指定何时使用资源的更多信息 提供资源Try removing the line:
from your onConfigurationChanged() function. And if that doesn't do the trick please post what you open/what happens when you click any of the tabs so we can realize what might be causing the problem.
However, the best way to do what you are trying there is to put the drawables in the correct directories under your resources and use the same name for both. This way you wouldn't need to specify what drawable to use for the different configuration, but rather leave the android OS to do that for you. Put the one in the directory
drawable
and the other one in thedrawable-land
ordrawable-port
depending on the orientation of your default one. You could read more about how to provide resources and specify when they are used in here Providing Resources感谢您的回复。现在我终于知道我的程序出了什么问题。
因为我使用的是TabHost,所以当屏幕方向改变时,只有两个Activity的onConfigurationChanged方法会被调用。
例如,有一个 Activity,它是 TabHost 的容器。还有其他四个 Activity(A、B、C 和 D),它们是 TabHost Activity 的一部分。
因此,当 Activity A 更改屏幕方向时,只会调用 TabHost 和 Activity A 的 onConfigurationChanged 方法。其他人不会被召唤。
这就是为什么当屏幕从纵向变为横向时。 A 的 onConfiguration 改变了,但是当你点击其他选项卡按钮时,B、C、D 不会改变(在横向模式下看起来仍然像纵向)。
所以答案是你必须控制TabHost Activity上的配置方法。现在效果很好。
如果您使用 Tabhost,则必须注意这一点。
Thanks for the replies. Now I finally know what's wrong with my program.
Because I were using the TabHost, when the screen orientation is changed, there are only two Activities' onConfigurationChanged method would be called.
For example, there is an Activity which is the container for TabHost. And there are other four Activities(A,B,C, and D) which are the parts of the TabHost Activity.
So when the screen orientation is changed with Activity A, only the TabHost and Activity A's onConfigurationChanged method would be called. The others will NOT be called.
That's why when the screen is changed from Portrait to Landscape. A's onConfiguration is changed, but when you click other tabs button, B,C, and D won't be changed(Still look like Portrait on Landscape mode).
So the answer is you have to control the configuration method on TabHost Activity. And now it works well.
If you are using Tabhost, you have to notice this.