我将桌面游戏从Windows Phone移植到Android,并且在Android布局系统上付出了很多努力。
这是Windows Phone上主页面的布局:
我有一个带有如下行定义的网格:
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
这意味着底部的控件在顶行的电路板完成所需的任何高度后都会得到。电路板尺寸在运行时自动更新,以使用其中最小的宽度和高度,使其成为正方形。
当使用RelativeLayout作为根布局时,我几乎得到了我想要的东西,但我的RelativeLayout作为根布局的问题是底部的控件最终会填满整个屏幕。
我已经多次尝试将TableLayout用作根布局,其中两个TableRows作为子项,其中第一个TableRow有android:height =&#34; match_parent&#34;或&#34; wrap_content&#34;第二个TableRow有android:height =&#34; wrap_content&#34;。即使我在运行时设置了电路板尺寸,Android也不像Windows Phone那样调整TableRow的大小,因此第一个TableRow占用了整个屏幕。
Android布局系统上的专家如何解决这样的问题?我在Google Play上发布的alpha版本在7&#34;平板电脑,但在具有高DPI屏幕的手机上,它看起来像这样:
更新 正如Luksprog在评论中建议的那样,我现在使用LinearLayout,第一个元素有android:layout_height =&#34; 0dp&#34;和android:layout_weight =&#34; 1&#34;第二部分有android:layout_height =&#34; wrap_content&#34;。起初它似乎并不像我想要的那样,因为底部的部分最终占据了整个屏幕,就像我之前曾多次尝试过的那样。
然而,在对&#34; wrap_content&#34;进行一些修改之后和&#34; match_parent&#34;片段内部元素的设置,我现在得到一个接近我想要的布局,并且它在高DPI手机上可以很好地扩展,只要我坚持使用px而不是所有位图的dp值(针对所有位置的建议) Android指南)。
这里是更新的布局和显示结果的屏幕截图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Board -->
<EivindApps.Ludo.Droid.Views.BoardView
android:id="@+id/boardView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<!-- Game info layout -->
<LinearLayout
android:id="@+id/gameInfoLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Left column -->
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<!-- Red player -->
<fragment
android:name="EivindApps.Ludo.Droid.Views.PlayerInfoFragment"
android:id="@+id/redPlayerInfoFragment"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<!-- Blue player -->
<fragment
android:name="EivindApps.Ludo.Droid.Views.PlayerInfoFragment"
android:id="@+id/bluePlayerInfoFragment"
android:layout_margin="5dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_width="wrap_content" />
</LinearLayout>
<!-- Middle column -->
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Dice -->
<fragment
android:name="EivindApps.Ludo.Droid.Views.DiceFragment"
android:id="@+id/diceFragment"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<!-- Right column -->
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<!-- Yellow player -->
<fragment
android:name="EivindApps.Ludo.Droid.Views.PlayerInfoFragment"
android:id="@+id/yellowPlayerInfoFragment"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/redPlayerInfoFragment" />
<!-- Green player -->
<fragment
android:name="EivindApps.Ludo.Droid.Views.PlayerInfoFragment"
android:id="@+id/greenPlayerInfoFragment"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/bluePlayerInfoFragment"
android:layout_width="wrap_content" />
</LinearLayout>
</LinearLayout>
<!-- End of game info layout -->
</LinearLayout>