简体   繁体   English

Android视图和自定义方向的更改,最好的处理方法是什么?

[英]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.

相关问题 Android应用重置方向更改,最好的处理方式? - Android app resets on orientation change, best way to handle? 蓝牙插座处理方向变化的最佳方法 - best way to handle orientation change with bluetooth sockets 处理方向更改的最佳做法:Android - Best practice to handle orientation change: Android 在Android中缩放关于方向更改的自定义视图 - Scaling Custom Views on orientation change in android 当自定义alertdialog具有打开的微调器时,处理方向更改的正确方法是什么? - What is the proper way to handle orientation change when a custom alertdialog has an open spinner? 处理方向变化时处理多个可交换片段的最佳方法 - Best way to handle multiple swappable Fragments on orientation change 处理方向更改android - Handle orientation change android 更改方向后处理大型列表数据集的最佳方法是什么? - What is the best way to handle a large list data set with orientation changing? Android:多个视图,深层导航,一个Activity。 什么是最好的处理方式? - Android: Multiple views, deep navigation, one Activity. What is the best way to handle? Android-添加有关方向更改的视图 - Android - Adding Views on orientation change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM