简体   繁体   English

片段:如何组成最终的,取决于屏幕尺寸的应用程序布局?

[英]Fragments: How do I compose the final, screen size dependent, layout of my application?

Edit: 编辑:

The question is ultimately about if there are any defacto standards or Google recommendations on how to implement the below question. 问题最终是关于是否存在事实上的标准或Google关于如何实施以下问题的建议。


Origin: 起源:

I have recently started to have a closer look at the Fragments concept in Android . 最近,我开始仔细研究Android中的Fragments概念 I do get the idea of it (or at least I think I do) and I also see the need for it. 我确实对此有想法(或者至少我认为是这样),并且我也看到了对此的需求。 I have no trouble understanding the technicalities behind the implementation of my fragments. 我很容易理解片段实现背后的技术。 What I seem to have problems understanding, though, is how to compose the final layout of my application, so that it automagically works on a large tablet as well as a small smartphone? 但是,我似乎在理解上有问题,是如何组成应用程序的最终布局,以便它可以在大型平板电脑和小型智能手机上自动运行?

I have my list fragment (eg MS Outlook-inbox-list) and I have my detail view fragment (eg MS Outlook-email-preview). 我有我的列表片段(例如MS Outlook-inbox-list)和详细视图片段(例如MS Outlook-email-preview)。 On a tablet I would naturally like to show these two fragments in a single activity, but on a smart phone I would like to show them in two different activities while there isn't necessarily room for both of them on a single activity. 在平板电脑上,我自然希望在单个活动中显示这两个片段,但是在智能手机上,我希望在两个不同的活动中显示它们,而在单个活动中并不一定要同时容纳两个片段。

How do I go about to solve this? 我该如何解决呢?

Do I have to implement three activities in my application (the all-in-one tablet activity and the two separate, smart-phone specific activities)? 我是否必须在应用程序中实现三个活动(多合一平板电脑活动和两个单独的智能手机特定活动)? But how can the application then decide on which implementation to use (1 or 1+1)? 但是,应用程序如何才能决定使用哪种实现(1或1 + 1)呢?

Usually my activities play the role of the 'controller' (according to the MVC paradigm) but, as of the above question, I would have three controllers. 通常,我的活动扮演“控制器”的角色(根据MVC范例),但是,就上述问题而言,我将拥有三个控制器。 Which construction would play the role of the root controller? 哪种构造将扮演根控制器的角色? Should I write a 'master activity' (without UI) which programmaticaly tries to decide which device it lives on and then call the suitable fragments activity accordingly? 我是否应该编写一个“主活动”(不带UI),以编程方式尝试确定其驻留在哪个设备上,然后相应地调用合适的片段活动? But then; 但是之后; how can I - in a well defined manner - find out what type of device (tablet/handset) my application is running on? 如何以一种定义明确的方式找出我的应用程序在哪种类型的设备(平板电脑/手机)上运行?

I guess I could post some of my code but I don't think it would clarify my problem any further as it's more a question of architectural choices than implementation specific. 我想我可以发布一些代码,但是我认为这不会进一步澄清我的问题,因为它更多是体系结构选择问题,而不是特定于实现的问题。


Cheers, 干杯,
dbm 数据库

Do I have to implement three activities in my application (the all-in-one tablet activity and the two separate, smart-phone specific activities)? 我是否必须在应用程序中实现三个活动(多合一平板电脑活动和两个单独的智能手机特定活动)?

No, probably only two. 不,可能只有两个。 One is responsible for loading the ListFragment and, if there is room, the detail fragment. 一个负责加载ListFragment ,如果有空间,则负责加载细节片段。 The other only loads the detail fragment -- this activity would be started by the first one if the user taps on a list entry and the detail fragment is not displayed by the first activity. 另一个仅加载详细信息片段-如果用户点击列表条目并且第一个活动未显示详细信息片段,则第一个活动将由第一个活动启动。

Usually my activities play the role of the 'controller' (according to the MVC paradigm) but, as of the above question, I would have three controllers. 通常,我的活动扮演“控制器”的角色(根据MVC范例),但是,就上述问题而言,我将拥有三个控制器。

IMHO, with fragments, the fragment is your controller. 恕我直言,带有片段的片段是您的控制者。 Activities are an orchestration layer determining which fragments are visible and handling cross-fragment events. 活动是业务流程层,用于确定哪些片段可见并处理跨片段事件。

But then; 但是之后; how can I - in a well defined manner - find out what type of device (tablet/handset) my application is running on? 如何以一种定义明确的方式找出我的应用程序在哪种类型的设备(平板电脑/手机)上运行?

The simplest solution in many cases is to drive it by layout XML resources. 在许多情况下,最简单的解决方案是通过布局XML资源来驱动它。

For example, in my above recommendation, the first activity could have two different versions of, say, main.xml . 例如,在我的上述建议中,第一个活动可能具有main.xml两个不同版本。 One ( res/layout/main.xml ) loads just the ListFragment . 一个( res/layout/main.xml )仅加载ListFragment The other (say, res/layout-large-land/main.xml ) loads the ListFragment and the detail fragment, or loads the ListFragment and a FrameLayout container for a dynamic detail fragment (one you would define via a FragmentTransaction ). 另一个(例如, res/layout-large-land/main.xml )加载ListFragment和详细信息片段,或加载ListFragment和动态详细信息片段的FrameLayout容器(您可以通过FragmentTransaction定义)。 The activity itself determines whether it is managing two fragments or one by determining if the second fragment (or its FrameLayout "slot") exists in the inflated main.xml . 活动本身通过确定第二个片段(或其FrameLayout “插槽”)是否存在于膨胀的main.xml来确定是管理两个片段还是一个片段。 That way, if you change your layout rules in the future (eg, res/layout-sw600dp-land/main.xml ), your Java code does not need to change. 这样,如果您将来更改布局规则(例如res/layout-sw600dp-land/main.xml ),则无需更改Java代码。

There is nothing stopping you from using Configuration and DisplayMetrics to try to make decisions in the Java code as well. 也没有什么可以阻止您使用ConfigurationDisplayMetrics尝试在Java代码中进行决策。

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

相关问题 如何准确计算相对于屏幕尺寸的布局(按钮)高度? - how do i accurately calculate my layout (button) height relative to screen size? 如何设置片段,以使我用于手机的布局也能与平板电脑一起正常使用? - How do I setup my fragments such that the layout I am using for phone also works well with tablets? 如何相对于屏幕尺寸调整表格布局中元素的尺寸? - How can I size the elements in my table layout relative to screen size? 我如何根据用户屏幕大小显示我的页面 - how do i display my page depending on the users screen size 当我在 Jetpack Compose 中显示多个片段的选项卡布局时如何显示/隐藏弹出窗口? - How to show/hide Popup when I have a Tab Layout with multiple fragments being displayed in Jetpack Compose? 如何在我的应用程序中创建此类聊天布局? - How do I create this type of chat layout in my application? 如何在同一布局中垂直加载两个片段? - How do I load two fragments vertically in the same layout? 片段和活动 - 我在哪里放置应用程序逻辑? - Fragments and Activities — where do I put my application logic? 如何从应用程序重置屏幕超时? - How do I reset screen timeout from my application? 由于屏幕旋转,如何处理碎片中的AsyncTask未完成 - How do I handle AsyncTask incompletion in fragments due to screen rotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM