【发布时间】:2014-06-30 04:34:16
【问题描述】:
我正在尝试编写一个应用程序,希望将手机锁定为纵向,但允许平板电脑版本同时具有横向和纵向。
我根据以下所有问题创建了一些几乎可以工作的东西...
How do I lock screen orientation for phone, but not for tablet? (Android) How to allow both landscape/portrait for tablets only Portrait for phone, landscape for Tablet (Android-Layout) landscape mode in tablet only Locking phone in portrait mode
到目前为止,我所做的是将 Activity 扩展到我自己的基本 Activity 中,并在 onCreate 和覆盖的 onConfigurationChanged 中包含方向更改代码
@Override
protected void onCreate(Bundle savedInstanceState) {
if(!(new DeviceHelper().isTablet(this)))
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
super.onCreate(savedInstanceState);
setupActionBar(getActivity());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
//don't reload the current page when the orientation is changed
if(!(new DeviceHelper().isTablet(this)))
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
super.onConfigurationChanged(newConfig);
}
一切正常(我重写了 onConfigurationChanged 以避免活动被破坏和重新创建,这会导致黑屏一秒钟)。
我的主要问题是,如果您以横向方式握住手机,那么活动仍然是纵向的……完美!但是,因为手机是实际横屏,所以使用横屏的布局,这是来自平板电脑的布局。
我的理解是,当调用setRequestedOrientation时,它应该立即触发onConfigurationChanged,但是,首先调用片段,然后布局不正确地膨胀,然后再调用onConfigurationChanged,这意味着横向布局已经设置。
我知道我可以根据配置在每个活动中设置布局,但我真的不想这样做。
那么我怎样才能让应用程序根据 setRequestedOrientation 使用适当 res 文件夹中的适当布局文件。
谢谢
【问题讨论】:
-
您将横向布局文件放在哪个文件夹中?
-
我有两个文件,一个在普通布局中,另一个在布局域中。使用 layout-land 文件是因为手机处于横向状态,即使 Activity 被锁定为纵向。
-
请看this回答希望这对某人有帮助
标签: android android-layout onconfigurationchanged