Android:为每个设备自动调整大小

Android: auto-resize for each device

我创建了一个非常简单的应用程序,并在 15 种不同的设备上下载了它。其中一些布局没有调整大小的问题,显示如下:

Layout that should be in all layouts

但是当我在 Sansung Galaxy Neo Plus 和平板电脑 Archos 101 Copper 中安装应用程序时,项目的布局不在我想要的位置。在平板电脑中,不同颜色的背景视图在 Galaxy Neo Plus 中是这样的: Layout in the Galaxy Neo Plus

要自动调整大小,在活动 xml 中,我使用了这些代码:

android:layout_marginTop="125dp"
android:layout_marginBottom="15dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"

设置按钮的边距 top/bottom/left/right

android:textSize="30dp"

设置按钮文本的大小

布局代码是这样的:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Al meglio di:"
    android:textSize="22dp"
    android:id="@+id/almegliodi"
    android:layout_alignParentTop="true"
    android:layout_marginTop="220dp"
    android:layout_centerHorizontal="true"
    android:typeface="serif"
    android:textStyle="bold|italic"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" UNO "
    android:textStyle="bold"
    android:textSize="35dp"
    android:textColor="@android:color/white"
    android:id="@+id/uno"
    android:background="@android:drawable/dialog_holo_dark_frame"
    android:onClick="uno"
    android:layout_below="@+id/almegliodi"
    android:layout_marginTop="30dp"
    android:layout_centerHorizontal="true"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" TRE "
    android:textStyle="bold"
    android:textSize="35dp"
    android:textColor="@android:color/white"
    android:id="@+id/tre"
    android:background="@android:drawable/dialog_holo_dark_frame"
    android:onClick="tre"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/uno"
    android:layout_marginTop="30dp"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" CINQUE "
    android:textStyle="bold"
    android:textSize="35dp"
    android:textColor="@android:color/white"
    android:id="@+id/cinque"
    android:background="@android:drawable/dialog_holo_dark_frame"
    android:onClick="cinque"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/tre"
    android:layout_marginTop="30dp"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ESCI"
    android:textStyle="bold"
    android:textSize="30dp"
    android:id="@+id/esci"
    android:background="@android:color/transparent"
    android:layout_marginBottom="7dp"
    android:layout_marginLeft="30dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:onClick="esci"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="INDIETRO"
    android:textStyle="bold"
    android:textSize="30dp"
    android:textColor="@android:color/white"
    android:id="@+id/indietro"
    android:background="@android:color/transparent"
    android:onClick="indietro"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_alignTop="@+id/esci"
    android:layout_marginRight="30dp"
    android:layout_marginBottom="7dp"
    />
</RelativeLayout

我在 google 上进行了搜索,但我发现使用 'dp' 是在所有设备中自动调整边距和文本大小的最佳方式。

我的错误是什么?我需要做什么来解决兼容性问题?

谢谢

这是您的布局的示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#005"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:layout_gravity="center_horizontal"
        android:src="@android:drawable/btn_star_big_on"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="24dp"
        android:layout_weight="1"
        android:background="#cfff"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"/>

        <android.support.v4.widget.Space
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"/>
    </LinearLayout>
</LinearLayout>

您当然应该为每个按钮使用 @string/ 以使您的应用程序可翻译,但该基本布局应该让您了解如何制作可正确缩放的布局。

基本上我使用一些线性布局,这些布局只是说明视图应该如何排列(水平或垂直)。然后我告诉包含三个主要按钮的视图,它的高度应该为零(只是为了性能),最重要的部分是布局权重为 1 (android:layout_weight="1"),告诉系统拆分打开的 space 到所有具有权重属性的视图。所以这个会尽可能地填满你的显示器。在我的案例中,其他视图仍将按照您的需要呈现 wrap_content