簡體   English   中英

針對不同的屏幕尺寸Android使用不同的布局

[英]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),無論大小如何,都在每個屏幕上顯示!

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 ...

}

來源: 如何使用代碼確定設備屏幕尺寸類別(小,標准,大,xlarge)?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM