[英]Using different layouts for different screen sizes Android
我一直在为Android开发一款记忆游戏,但在布局方面遇到了麻烦。
对于每种类型的游戏(简易,中型和硬型),我都有3种不同的布局,其中在屏幕上需要匹配4x4、5x5或6x6图像。
我正在使用ImageAdapter获取图像并填充用于在屏幕上显示iamges的GridView。
这是Easy游戏的XML文件(4x4图片):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/mainBar"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/player1"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player1 - "
/>
<TextView
android:id="@+id/player1Score"
android:layout_toRightOf="@+id/player1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00 "
/>
<TextView
android:id="@+id/player2Score"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
/>
<TextView
android:id="@+id/player2"
android:layout_toLeftOf="@+id/player2Score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player2 - "
/>
<Chronometer
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="02:00" />
</RelativeLayout>
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_below="@id/mainBar"
android:gravity="center_vertical|center_horizontal"
android:numColumns="4">
</GridView>
</RelativeLayout>
唯一的问题是,当我在屏幕较小的仿真器上运行应用程序时,图像看起来像是张紧的……(请参阅IMG#1)。请参阅IMG#2),无论大小如何,都在每个屏幕上显示!
我正在使用不同的资源(ldpi,mdpi,hdpi的不同图像)。
您遇到的问题是,当您支持多种屏幕密度时,却不支持多种物理屏幕尺寸。 这就是为什么在较小的屏幕上图像看起来较大并开始变形。
为了解决这个问题,您可以为每种电话尺寸(小,标准,大和超大)分别进行布局。
但是,由于这很繁琐,所以我倾向于使用线性布局和权重,以便布局xml可以在所有电话尺寸上缩放。
在这种情况下,您可能希望制作一个垂直线性布局,并在其中放置4个水平线性布局。
使用它来找到正确的物理屏幕尺寸:
if ((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_LARGE) {
// on a large screen device ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.