Android的方向变化:不同的布局,相同片段 [英] Android Orientation Change: Different Layout, Same Fragments

查看:186
本文介绍了Android的方向变化:不同的布局,相同片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是pretty的很多经典的Andr​​oid的使用案例。

This is pretty much a classic Android use case.

假设我们有2个片段:碎裂和FragmentB

Say we have 2 fragments: FragmentA and FragmentB.

在横向模式下,碎裂和FragmentB并肩而坐。

In landscape mode, FragmentA and FragmentB sit side by side.

在纵向模式下,他们各自占据整个屏幕在使用的时候。

In portrait mode, they each take up the full screen when in use.

(见这一形象,但更换tablet->景观和handset->人像)

(See this image but replace tablet->landscape and handset->portrait)

按此处的说明(的支持单窗格和多窗格布局),有2种方式来实现这一目标:

As explained here (Supporting Single-Pane and Multi-Pane Layouts), there are 2 ways to achieve this:

1的多个片段,一个活动:使用一个活动,无论设备的大小,但在运行时决定是否合并片段布局(以创建多窗格设计)或交换片段(创建一个单窗格设计)。

1- Multiple fragments, one activity: Use one activity regardless of the device size, but decide at runtime whether to combine fragments in the layout (to create a multiple-pane design) or swap fragments (to create a single-pane design).

2 - 多个片段,多重活动:在平板电脑上,将多个片段在一个活动的;在手机上,使用单独的活动举办的每个片段。例如,当片剂设计使用两个片段在一个活动,用于手机相同的活性,但提供的替代布局只包括第一片段。在手机上运行,​​当你需要切换片段(当用户选择一个项目,如),开始承载第二个片段的另一个活动。

2- Multiple fragments, multiple activities: On a tablet, place multiple fragments in one activity; on a handset, use separate activities to host each fragment. For example, when the tablet design uses two fragments in an activity, use the same activity for handsets, but supply an alternative layout that includes just the first fragment. When running on a handset and you need to switch fragments (such as when the user selects an item), start another activity that hosts the second fragment.

这使得很多在理论意义上的,但我打的一些障碍,试图真正实现无论这些方法在某种程度上恢复片段的状态的方向变化和后退按钮$时p $ PSS

This makes a lot of sense in theory, but I'm hitting some hurdles in trying to actually implement either of these approaches in a way that restores the state of the Fragments when the orientation changes and on back button press.

我尝试了第一种方法,得到了很远,但发现它凌乱,因为它需要手动管理所有片段的交易,特别是因为一个片段的容器不能轻易改变。这也是一个痛苦的决定怎么做pssed回$ P $,因为在backstack最后一笔交易可能属于另一个方向。

I tried the first approach and got quite far, but found it messy because it required managing all the fragment transactions manually, in particular because the container of a Fragment cannot be changed easily. It is also a pain to decide what to do on back pressed because the last transaction on the backstack might belong to the other orientation.

现在我想第二个选项。这似乎更清洁,但迄今为止的碎片得到从头开始重新在每个方向变化(因为每个方向使用不同的活动)。 我想有一种方法来恢复从另一活动/方向的片段的状态。

Now I am trying the second option. It seems cleaner so far but the Fragments get recreated from scratch on every orientation change (because each orientation uses a different Activity). I would like to have a way to restore the Fragment state from the other Activity/orientation.

任何人都可以解释如何可以做到这一点,或点我到一个合适的教程或示例应用程序?

Can anyone explain how this can be done, or point me to an appropriate tutorial or sample application?

推荐答案

  • MainActivity 其中片段是取代添加该配置 <活动机器人:MainActivity名称=机器人:configChanges =定位|屏幕尺寸|键盘| keyboardHidden 机器人:标签=@字符串/ APP_NAME> <意向滤光器> <作用机器人:名称=android.intent.action.MAIN/> <类机器人:名称=android.intent.category.DEFAULT/>< /意图滤光器> < /活动
  • 您已经创建片段的景观布局的碎裂 FragmentB
  • 如果您需要更改这两个片段的行为,你应该改变 onConfigurationChanged 片段的事件。

  • add this configuration in MainActivity where Fragment are replace <activity android:name=".MainActivity" android:configChanges="orientation|screenSize|keyboard|keyboardHidden" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT"/></intent-filter> </activity
  • You have create fragment landscape layout for FragmentA and FragmentB
  • if you need to change behavior of both fragment you should change in onConfigurationChanged event of fragment.
  • @覆盖     公共无效onConfigurationChanged(配置NEWCONFIG){         super.onConfigurationChanged(NEWCONFIG);         如果(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)         {             //写你的东西的横向         }否则,如果(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)         {             //写你的东西为纵向         }     }

    @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { //write your stuff for landscape orientation }else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { //write your stuff for portrait orientation } }

    这篇关于Android的方向变化:不同的布局,相同片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆