支持带有动态或静态片段的不同屏幕尺寸? [英] Supporting different screen sizes with dynamic or static fragments?

查看:118
本文介绍了支持带有动态或静态片段的不同屏幕尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现能够使用片段支持多屏幕令人困惑.我以为这个想法是我可以提供该活动的多个版本,并且该活动可以静态嵌入1个或多个片段(取决于房地产).

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.

支持的动态片段,这些片段是从代码中添加的,而不是嵌入式的< fragment> 元素类型.由于我不知道活动中xml的配置详细信息,因此如何在代码中完成此操作.

Supporting dynamic fragments where these are added from code rather than embedded <fragment> element types. How would this be done in code as I wouldn't know the configuration details of the xml from the activity.

有人有这样的例子吗?

何时使用静态或动态片段.

When would I use static or dynamic fragments.

我可以预见到使用动态片段的原因之一是支持保留片段,因此活动可以只在代码中要求它,但是我发现很难理解如果活动(java)不能确切知道如何支持该片段可以使用哪些xml文件

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

谢谢

推荐答案

要回答有关如何使用动态片段实现双重窗格的问题,方法如下:

To answer your question on how to implement the dual pane with dynamic fragments, here is how:

  1. 在您的资源中创建一个名为values-land的文件夹.在此文件夹中,添加一个config.XML文件,其中将具有以下代码:

  1. Create a folder named values-land in your resources. In this folder, add a config.XML file in which you will have a code like this:

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

在普通"值文件夹中的config.xml文件中,添加以下代码:

In the "normal" values folder, in the config.xml file, add this code:

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

在您的活动中,您可以在onCreate()中检测与刚创建的景观配置相对应的布尔值:

In your activity, you can detect in the onCreate() the boolean value corresponding to landscape config you just created:

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

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

然后基于布尔值,您可以决定仅在两个顶部的两个片段容器(单窗格布局)或彼此相邻的两个片段容器(双窗格布局)的基础上膨胀一个布局.

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).

最后将片段动态地添加到适当的容器中,瞧.

Finally add your fragments dynamically to the appropriate container and voila.

我喜欢这个方法,因为它非常类似于Android,使用了values文件夹.同样,您也不必为现有的片段"模式进行太多更改即可使其支持双窗格模式.

I like this approch because it is very Android like, by the use of the different values folder. Also you don't have to change much to an existing working Fragment pattern to make it support dual pane mode.

这篇关于支持带有动态或静态片段的不同屏幕尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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