简体   繁体   English

不同屏幕尺寸的片段

[英]Fragments for different screen sizes

I'm writing a turn based game for Android. 我正在为Android开发回合制游戏。 The main game has 3 sections. 主游戏分为3个部分。 An introduction screen showing which player is to play next, the actual game screen where the player plays for points, and a board screen showing the rankings of the players. 介绍屏幕显示了接下来要玩的玩家,游戏玩家实际玩游戏时会在该屏幕上争分,而桌面屏幕则显示了玩家的排名。

In my implementation of this, I use 3 fragments (one for each section) and navigate between them using fragment transactions (FragmentTransaction.replace) as required. 在此的实现中,我使用3个片段(每个部分一个),并根据需要使用片段事务(FragmentTransaction.replace)在它们之间导航。

This approach works fine, however I'd like to now add an extra feature, in that for large screen devices, the introduction screen, and the board screen would be displayed simultaneously. 这种方法很好用,但是我现在想添加一个额外的功能,对于大屏幕设备,将同时显示简介屏幕和桌面屏幕。 I understand how to do this in the xml, so when we first navigate to the introduction screen, we have both fragments displayed. 我了解如何在xml中执行此操作,因此,当我们第一次导航到简介屏幕时,我们同时显示了两个片段。

The problem I have relates the fragment transactions. 我遇到的问题与片段事务有关。 When performing the transaction to pass out of the board screen, I need to determine whether to transition to a two fragment layout, or a one fragment layout? 在执行事务以传递到板外屏幕时,我需要确定是过渡到两个片段布局还是一个片段布局? What is the best way to do this? 做这个的最好方式是什么? Do I have to programmatically get the screen size before determining what transactions to perform, or is there a neater (XML?) way of doing this? 在确定要执行哪些事务之前,我是否必须以编程方式获取屏幕大小,或者是否有更整洁的方法(XML?)?

Thanks, 谢谢,

Will

Yes, there is the XML-way of doing this. 是的,有XML的方式可以做到这一点。 But it will need some refactoring. 但这需要一些重构。

Create different layouts with FrameLayout containers. 使用FrameLayout容器创建不同的布局。 Phone-Layout usually contains only one container. 电话布局通常仅包含一个容器。 Tablet-Layout two or three. 平板电脑布局为二或三。

You have to place the layouts according to the screensizes into proper directory: 您必须根据屏幕尺寸将布局放置在适当的目录中:

res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)

see here: Layout for tablets in Android 参见此处: Android中平板电脑的布局

Then put the fragments into container, if container exists , like user did it here: Trying to add a fragment to my fragment container FrameLayout 然后将片段放入容器中( 如果container存在) ,就像用户在此处所做的那样: 尝试将片段添加到片段容器FrameLayout中

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

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