【发布时间】:2016-01-11 19:36:28
【问题描述】:
我的问题:
对于某些要求,我的活动需要两种不同的 xml 布局:
- 一个用于横向模式。
- 还有一个用于横向反转模式(横向倒置)。
不幸的是,Android 不允许为横向反转创建单独的布局(就像我们可以使用 layout-land 和 layout-port 为纵向和横向创建一样)。
AFAIK,唯一的方法是从 java 代码中更改 activity-xml。
我试过的:
1) 覆盖onConfigurationChanged() 方法来检测方向变化,但我不知道它是横向还是横向:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d("TEST","Landscape");
}
}
(在清单中我的活动标签中带有android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection")
2) 按照this answer 中的建议使用OrientationEventListener 和SENSOR_DELAY_NORMAL,但在进入我的if 块之前设备方向会发生变化,因此我会延迟更新视图:
mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){
@Override
public void onOrientationChanged(int orientation) {
if (orientation==0){
Log.e("TEST", "orientation-Portrait = "+orientation);
} else if (orientation==90){
Log.e("TEST", "orientation-Landscape = "+orientation);
} else if(orientation==180){
Log.e("TEST", "orientation-Portrait-rev = "+orientation);
}else if (orientation==270){
Log.e("TEST", "orientation-Landscape-rev = "+orientation);
} else if (orientation==360){
Log.e("TEST", "orientation-Portrait= "+orientation);
}
}};
我的问题:
在"Landscape" 和"Landscape-reverse" 方向之间更改活动布局是否有更好的解决方案?
非常感谢任何建议。
【问题讨论】:
标签: android orientation landscape device-orientation orientation-changes