【问题标题】:Via rotate orientation change layout without reload OnCreate()通过旋转方向更改布局而不重新加载 OnCreate()
【发布时间】:2015-06-02 15:14:45
【问题描述】:

我在不同的文件夹中有 3 个布局:

1.layout/HomeLayout.axml(其中 layout 是布局文件夹,HomeLayout.axml 是全局 axml 文件)。
2.layout-large-port/HomeLayout.amxl(其中layout-large-port是大(显示尺寸)纵向的文件夹,HomeLayout.axml是axml文件)。
3.layout-large-land/HomeLayout.axmllayout-large-land 是大(显示尺寸)景观的文件夹,HomeLayout.axml 是 axml文件)。

在这个活动中,我得到了带有一些数据的 ListView,当我将手机从 portrait 旋转到 landscape > ,我需要保存我在 Portrait 模式下制作的所有内容(listview 上的每一行都有不同的东西,如 buttons/textview 等)并显示横向

那么现在做了什么。在 HomeActivity 我声明了这一点:

ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation

这个 sn-p 效果很好,但有一个大问题:旋转后(从 PortraitLandscape)仍然是布局 Portrait。我该如何解决?

我也尝试覆盖方法OnConfigurationChanged

 public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
        {
            base.OnConfigurationChanged (newConfig);

            if(newConfig.Orientation == Android.Content.Res.Orientation.Landscape)
                {
                   SetContentView(Resource.Layout.ProductsLayout); //system understand that now is Landscape and put correct layout;  
                }
            else
                {
                    SetContentView(Resource.Layout.ProductsLayout); //same here
                }  

所以这个方法对我没有帮助,因为当我旋转手机时,布局设置正确但没有数据(列表视图为空),之后,当我试图从 横向返回时纵向,布局设置正确但列表为空。
有什么建议吗?

PS 对不起我的英语!

【问题讨论】:

    标签: android xamarin mono orientation


    【解决方案1】:

    当配置更改时,Android 将彻底销毁并重新创建您的 Activity;该活动中的所有视图及其持有的数据也将被丢弃。

    来自Android docs

    警告:您的活动将在每次 用户旋转屏幕。当屏幕改变方向时, 系统销毁并重新创建前台活动,因为 屏幕配置已更改,您的活动可能需要加载 替代资源(例如布局)。

    在配置更改之间持久化数据的标准机制是使用ActivityOnSaveInstanceState 回调保存视图数据,然后使用通过OnCreate 回调提供的包恢复数据。

    这是一个粗略的例子:

    public class MainActivity : Activity
    {
        public const string ARG_LIST_STATE = "list_state";
        ListView _listView;
    
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
    
            SetContentView (Resource.Layout.Main);
    
            if (bundle != null) {
                // Existing data to restore!
                if (bundle.ContainsKey (ARG_LIST_STATE)) {
                    var listState = bundle.GetStringArray (ARG_LIST_STATE);
                    // TODO: Restore state into list view.
                }
            }
        }
    
        protected override void OnSaveInstanceState (Bundle outState)
        {
            string[] listState= new string[5]; // TODO: Grab data from the list view and serialize it into the outState
            outState.PutStringArray(ARG_LIST_STATE, listState);
    
            base.OnSaveInstanceState (outState);
        }
    }
    

    Xamarin here 提供了另一个示例。

    我强烈建议您查看activity lifecycle docs,以便您更好地了解 Android 如何管理活动。

    【讨论】:

    • 感谢您的回复。是的,我知道这种机制“onSaveInstance”,但我需要替代方案(我如何在我的问题上写,不重新加载 OnCreate 并在横向布局上显示所有数据)。那么这可能吗?
    【解决方案2】:

    解决方案是(对我而言)为肖像设计可接受的设计,即它与横向模式没有区别(只需使用 sum_weight 等机制)。 p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多