簡體   English   中英

支持屏幕尺寸/密度的Android應用

[英]Android app supporting screen sizes/densities

我最近寫了一個小型的android應用程序,很難在布局上擴展到其他設備上。 我為它編寫並使用我的Galaxy S3對其進行了測試。

要在其他設備上進行測試,我必須使用AVD。 我使用布局文件並為每個屏幕密度(values-mdpi,values-hdpi等)提供dimens.xml。

這僅適用於我正在測試的AVD的屏幕尺寸。 but a different screen , the layout gets whacky. 如果我使用的AVD具有相同的屏幕但屏幕不同,則布局會出現問題。 我要為我要支持的每個設備提供全套尺寸嗎?

例如我要支持的每種組合的值-(屏幕尺寸)-(屏幕密度)-(方向)

這是我的activity_main.xml布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:click="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >

<click.CustomFontTextView
    android:id="@+id/click_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/textView_title_margintop"
    android:gravity="center_horizontal"
    android:text="@string/app_name"
    android:textColor="@color/black"
    android:textSize="@dimen/textView_title_textsize"
    click:fontName="freescript.ttf" />

<Button
    android:id="@+id/button_newgame"
    style="@style/clickMenuButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/button_newgame_margintop"
    android:onClick="newGame"
    android:text="@string/button_newgame" />

<Button
    android:id="@+id/button_highscore"
    style="@style/clickMenuButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/button_highscore_margintop"
    android:onClick="highScore"
    android:text="@string/button_highscore" />

<Button
    android:id="@+id/button_quit"
    style="@style/clickMenuButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/button_quit_marginTop"
    android:onClick="showInsertitial"
    android:text="@string/button_quit" />

</FrameLayout>

和來自values-xhdpi /的dimens.xml

<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>

<!-- Main Activity Layout -->
<dimen name="textView_title_textsize">145sp</dimen>
<dimen name="textView_title_margintop">125dp</dimen>
<dimen name="button_newgame_margintop">390dp</dimen>
<dimen name="button_highscore_margintop">455dp</dimen>
<dimen name="button_quit_marginTop">520dp</dimen>

<!-- Game Activity Layout -->
<dimen name="textView_timer_textsize">80sp</dimen>
<dimen name="textView_timer_margintop">80dp</dimen>
<dimen name="textView_clickcount_textsize">50sp</dimen>
<dimen name="textView_clickcount_margintop">510dp</dimen>

<!-- Dialog Layout -->
<dimen name="textView_dialogTitle_textsize">60sp</dimen>
<dimen name="textView_dialogTitle_padding">6dp</dimen>
<dimen name="textView_dialogGOInfo_textsize">40sp</dimen>
<dimen name="textView_dialogGOInfo_padding">10dp</dimen>
<dimen name="textView_dialogHSInfo_textsize">70sp</dimen>
<dimen name="textView_dialogHSInfo_padding">5dp</dimen>

<!-- Menu Button -->
<dimen name="button_menubutton_textsize">15sp</dimen>
</resources>

有任何簡單的解決方法,還是我必須寫一百萬份dimens.xml?

更好地為Galaxy S3使用Linearlayout屏幕尺寸分辨率為720 * 1280 XHDPI,因此您需要在res文件夾中創建一個名為layout-xlarge或layout-sw320dp的文件夾。 希望這會有所幫助。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:click="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <click.CustomFontTextView
        android:id="@+id/click_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/textView_title_margintop"
        android:gravity="center_horizontal"
        android:text="@string/app_name"
        android:textColor="@color/black"
        android:textSize="@dimen/textView_title_textsize"
        click:fontName="freescript.ttf" />

    <Button
        android:id="@+id/button_newgame"
        style="@style/clickMenuButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/button_newgame_margintop"
        android:onClick="newGame"
        android:text="@string/button_newgame" />

    <Button
        android:id="@+id/button_highscore"
        style="@style/clickMenuButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/button_highscore_margintop"
        android:onClick="highScore"
        android:text="@string/button_highscore" />

    <Button
        android:id="@+id/button_quit"
        style="@style/clickMenuButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/button_quit_marginTop"
        android:onClick="showInsertitial"
        android:text="@string/button_quit" />

    </LinearLayout>

更新1

為不同的屏幕尺寸提供不同的布局默認情況下,Android會調整應用程序布局的大小以適合當前設備的屏幕。 在大多數情況下,這可以正常工作。 在其他情況下,您的UI可能看起來不那么好,可能需要針對不同的屏幕尺寸進行調整。 例如,在較大的屏幕上,您可能需要調整某些元素的位置和大小以利用額外的屏幕空間,或者在較小的屏幕上,可能需要調整大小以使所有內容都適合屏幕。
可用於提供特定於大小的資源的配置限定符為small,normal,large和xlarge。 例如,超大屏幕的布局應放在layout-xlarge /中。

從Android 3.2(API級別13)開始,不建議使用上述大小組,而應使用sw <N> dp配置限定符定義布局資源所需的最小可用寬度。 例如,如果您的多窗格平板電腦布局需要至少600dp的屏幕寬度,則應將其放置在layout-sw600dp /中 關於聲明Android 3.2平板電腦布局的部分中,將進一步討論使用新技術聲明布局資源。

在Android 開發者網站上 -只需使用layout-small / layout-xlarge /等。

我通過從FrameLayout更改為LinearLayout並使用android:layout_weight來維持小部件在所有屏幕尺寸和密度上的位置,從而解決了該問題。

暫無
暫無

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

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