简体   繁体   English

针对不同的屏幕尺寸Android使用不同的布局

[英]Using different layouts for different screen sizes Android

I've been working on a Memory Game for Android and I'm having a litle problem with tha layout. 我一直在为Android开发一款记忆游戏,但在布局方面遇到了麻烦。

I have 3 diferent layouts for every type of game (easy, medium and hard) where I have 4x4, 5x5 or 6x6 images on the screen that need to be matched. 对于每种类型的游戏(简易,中型和硬型),我都有3种不同的布局,其中在屏幕上需要匹配4x4、5x5或6x6图像。

I'm using an ImageAdapter to get the images and fill the GridView that I'm using for displaying the iamges on the screen. 我正在使用ImageAdapter获取图像并填充用于在屏幕上显示iamges的GridView。

Here's the XML file for the Easy game (4x4 images): 这是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>

The only problem is that when I'm running the app on an emulator with a small screen size it the images look streched....(see IMG#1)..when I would really want to look something like this..(see IMG#2), on every screen no matter of the size! 唯一的问题是,当我在屏幕较小的仿真器上运行应用程序时,图像看起来像是张紧的……(请参阅IMG#1)。请参阅IMG#2),无论大小如何,都在每个屏幕上显示!

IMG#1

IMG#2

I'm using different resources (different images for ldpi, mdpi, hdpi). 我正在使用不同的资源(ldpi,mdpi,hdpi的不同图像)。

The problem you are having is that while you are supporting multiple screen densities you are not supporting multiple physical screen sizes. 您遇到的问题是,当您支持多种屏幕密度时,却不支持多种物理屏幕尺寸。 That is why on the smaller screen your images look bigger and begin to distort. 这就是为什么在较小的屏幕上图像看起来较大并开始变形。

To solve this you can make a seperate layout for each phone size: small, normal, large and xlarge. 为了解决这个问题,您可以为每种电话尺寸(小,标准,大和超大)分别进行布局。

However as that is quite tedious I tend to lean towards using a linear layout and weights so that the layout xml scales on all phone sizes. 但是,由于这很繁琐,所以我倾向于使用线性布局和权重,以便布局xml可以在所有电话尺寸上缩放。

In this case you would want to make one vertical linear layout with 4 horizontal linear layouts inside. 在这种情况下,您可能希望制作一个垂直线性布局,并在其中放置4个水平线性布局。

Use this to find the correct physical screen size: 使用它来找到正确的物理屏幕尺寸:

if ((getResources().getConfiguration().screenLayout & 
    Configuration.SCREENLAYOUT_SIZE_MASK) == 
        Configuration.SCREENLAYOUT_SIZE_LARGE) {
    // on a large screen device ...

}

Source: How to determine device screen size category (small, normal, large, xlarge) using code? 来源: 如何使用代码确定设备屏幕尺寸类别(小,标准,大,xlarge)?

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

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