[英]Android views and custom orientation change, what's the best way to handle?
I've an android app that has multiple views. 我有一个具有多个视图的android应用。 I've disabled the automatic orientation switching by declaring
android:configChanges="keyboardHidden|orientation|screenSize"
for my activity. 我通过为我的活动声明
android:configChanges="keyboardHidden|orientation|screenSize"
来禁用了自动方向切换。
Q1) How I should re-activate views in onConfigurationChanged()
? Q1)我应该如何重新激活
onConfigurationChanged()
视图? It seems I need to reset the content view using setContentView
method. 看来我需要使用
setContentView
方法重置内容视图。 Is there an alternative way to signal view that now you should inflate the landscape/portrait version of the layout? 有没有其他方法可以发出信号通知您现在应该为布局的横向/纵向版本充气? Some of my views contain quite complicated
states/modeless
dialogs etc that can appear any time so deleting the view object and re-instantiating it just doesn't sound right. 我的某些视图包含非常复杂的
states/modeless
对话框等,它们可以随时出现,因此删除视图对象并重新实例化听起来并不正确。
Q2) What's the best way in onConfigurationChanged()
to know which view we should actually now activate ie what was focused when orientation change was initiated? Q2)在
onConfigurationChanged()
中知道我们现在应该实际激活哪个视图的最佳方法是什么,即,在启动方向更改时聚焦了什么? I'm reluctant to rely on getCurrentFocus()
as it can be null sometimes. 我不愿依赖
getCurrentFocus()
因为有时它可能为null。
Q1) How I should re-activate views in onConfigurationChanged()?
Q1)我应该如何重新激活onConfigurationChanged()中的视图?
As you mentioned, setContentView()
is a good way to do this. 正如您提到的,
setContentView()
是执行此操作的好方法。 Just remember to not always pass in a layout XML to this method. 请记住不要总是将布局XML传递给此方法。 Instead, pre-inflate your layout into a
View
object and pass in that View object to setContentView. 而是将您的布局预先充气到
View
对象中,然后将该View对象传递给setContentView。 That way, you don't incur the cost of re-creating your view object on every orientation change. 这样,您就不会在每次方向更改时都产生重新创建视图对象的费用。
The following is a sample code - may not work as-is; 以下是示例代码-可能无法按原样运行; but meant to illustrate my point.
只是想说明我的观点。
View mLandscapeView;
View myPortraitView
protected void onCreate(Bundle bundle){
View myLandscapeView = getLayoutInflater().inflate(R.layout.my_landscape, null);
View myPortraitView = getLayoutInflater().inflate(R.layout.my_portrait, null);
//...
}
@Override
protected void onConfigurationChanged(Configuration config){
if(config.orientation = Configuration.ORIENTATION_LANDSCAPE){
//adjust mLandscapeView as needed
setContentView(mLandscapeView);
}
// And so on ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.