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

时间:2014-07-14 09:11:29

标签: android xml android-layout layout

我最近写了一个小的Android应用程序,并且很难在其他设备上进行布局扩展。我写了它并用我的Galaxy S3测试它。

要在其他设备上测试,我必须使用AVD。我使用布局文件并为每个屏幕密度提供dimens.xml(values-mdpi,values-hdpi等)。

这仅适用于我正在测试的AVD的屏幕尺寸。如果我使用具有相同屏幕 密度 但具有不同屏幕 尺寸 的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?

2 个答案:

答案 0 :(得分:0)

更好地使用Linearlayout对于Galaxy S3屏幕尺寸分辨率为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&lt; N&gt; dp配置限定符来定义布局资源所需的最小可用宽度。例如,如果多窗格平板电脑布局需要至少600dp的屏幕宽度,则应将其放在 layout-sw600dp / 中。使用新技术声明布局资源将在有关为Android 3.2声明平板电脑布局的部分中进一步讨论。

来自Android developer site - 只需使用layout-small / layout-xlarge / etc ..文件夹:=) 有史以来最好的方式..

答案 1 :(得分:0)

我通过从FrameLayout更改为LinearLayout并使用android:layout_weight来维护所有屏幕尺寸和密度的窗口小部件位置来解决问题。

相关问题