简体   繁体   English

支持带有动态或静态片段的不同屏幕尺寸?

[英]Supporting different screen sizes with dynamic or static fragments?

I am found it confusing to be able to support multi screens using fragments. 我发现能够使用片段支持多屏幕令人困惑。 I thought the idea was that i could offer more than one version of the activity and that activity could have 1 or more fragments (depending on real estate) statically embedded. 我以为我可以提供该活动的多个版本,并且该活动可以静态嵌入1个或多个片段(取决于房地产)。

Supporting dynamic fragments where these are added from code rather than embedded <fragment> element types. 支持动态片段,这些片段是通过代码而不是嵌入式<fragment>元素类型添加的。 How would this be done in code as I wouldn't know the configuration details of the xml from the activity. 由于我不知道活动中xml的配置详细信息,因此如何用代码完成此操作。

Does anyone have an example of this ? 有没有人举这个例子?

When would I use static or dynamic fragments. 什么时候使用静态或动态片段。

One reason I could foresee using dynamic fragments is to support the retaining on the fragment so the activity could just ask for it in code but I am finding it difficult to understand how this would be supported without the activity (java) knowing exactly what xml files are available 我可以预见使用动态片段的原因之一是支持保留片段,因此活动可以只在代码中要求它,但是我发现很难理解,如果活动(java)不能确切知道什么xml文件,将如何支持该片段。可用

Thanks 谢谢

I found it very useful to look through and understand the code of the Android Samples . 我发现通读并理解Android示例代码非常有用。

These sample projects are usually quite small and focus on only 1 or 2 main concepts each. 这些示例项目通常很小,并且每个项目仅关注1或2个主要概念。

If you don't want to actually download and install the sample projects, you can also just browse the source code on GitHub . 如果您不想实际下载并安装示例项目,也可以在GitHub上浏览源代码。

For example in the NavigationDrawer sample the NavigationDrawerActivity class contains a dynamically created PlanetFragment class. 例如,在NavigationDrawer示例中NavigationDrawerActivity类包含一个动态创建的PlanetFragment类。

There are 2 main reasons to use a "dynamic" fragment: 使用“动态”片段有两个主要原因:

  1. You can pass parameters by code. 您可以通过代码传递参数。 For example, if you have a Fragment that displays an article, you create the Fragment by code and initialize it with the Article to display (using Fragment.setArguments(Bundle) ). 例如,如果您有一个显示文章的Fragment,则可以通过代码创建Fragment并使用要显示的Article对其进行初始化(使用Fragment.setArguments(Bundle) )。 This is the equivalent of passing parameters to an Activity using Intent.putExtras() . 这等效于使用Intent.putExtras()将参数传递给Activity。
  2. You can interchange different fragments in the same container. 您可以在同一容器中交换不同的片段。

Example 1: in your main Activity you have a navigation menu with 3 sections. 示例1:在主活动中,您有一个包含3个部分的导航菜单。 When the user chooses a new section, you replace Fragment "Section A" with Fragment "Section B" in the same FrameLayout container. 当用户选择一个新的节时,您可以在同一FrameLayout容器中用“节B”替换“节A”。

Example 2: You display a two-pane layout on tablet (items list + details). 示例2:您在平板电脑上显示了两个窗格的布局(项目列表+详细信息)。 Each time the user selects an item in the left list, you create a new Fragment initialized for the new item to display and replace the existing Fragment on the right with the new one. 每次用户在左侧列表中选择一个项目时,您都会为该新项目创建一个新的Fragment进行初始化,以显示该新项目并将其右侧的现有Fragment替换为新的Fragment。

Example 3: You have a ViewPager which displays a long list of articles. 示例3:您有一个ViewPager,其中显示了很长的文章列表。 You swipe right or left to navigate to the previous/next article. 您向右或向左滑动即可浏览到上一篇/下一篇文章。 These fragments will be created programmatically inside a FragmentPagerAdapter; 这些片段将在FragmentPagerAdapter内以编程方式创建; you can't do that using static Fragments. 您无法使用静态片段来做到这一点。

To answer your question on how to implement the dual pane with dynamic fragments, here is how: 要回答有关如何使用动态片段实现双重窗格的问题,方法如下:

  1. Create a folder named values-land in your resources. 在资源中创建一个名为values-land的文件夹。 In this folder, add a config.XML file in which you will have a code like this: 在此文件夹中,添加一个config.XML文件,其中将具有以下代码:

    <resources> <item type="bool" name="dual_pane">true</item> </resources>

  2. In the "normal" values folder, in the config.xml file, add this code: 在“普通”值文件夹中的config.xml文件中,添加以下代码:

    <resources> <item type="bool" name="dual_pane">false</item> </resources>

  3. In your activity, you can detect in the onCreate() the boolean value corresponding to landscape config you just created: 在您的活动中,您可以在onCreate()中检测与刚创建的景观配置相对应的布尔值:

    getResources().getBoolean(R.bool.dual_pane); getResources()。getBoolean(R.bool.dual_pane);

  4. Then based on the boolean value, you can decide to inflate a layout with just two fragment containers on top of the other (single pane layout) or with two fragment containers next to each other (dual pane layout). 然后,基于布尔值,您可以决定仅在两个顶部的两个片段容器之间放置一个分片容器(单窗格布局),或者彼此相邻的两个片段容器之间进行充气(双窗格布局)。

  5. Finally add your fragments dynamically to the appropriate container and voila. 最后,将片段动态添加到适当的容器中,瞧。

I like this approch because it is very Android like, by the use of the different values folder. 我喜欢这个方法,因为它非常类似于Android,使用了values文件夹。 Also you don't have to change much to an existing working Fragment pattern to make it support dual pane mode. 另外,您不必对现有的工作Fragment模式进行太多更改即可使其支持双窗格模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM