我创建了一个非常简单的应用程序,我在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"
设置按钮'从上/下/左/右
的边距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
我在谷歌搜索但我发现使用了&#39; dp&#39;是在所有设备中自动调整边距和文本大小的最佳方法。
我的错误是什么?我需要做些什么来解决兼容性?
谢谢
答案 0 :(得分:0)
以下是您的布局如何显示的示例:
<?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"
)的最重要部分,它告诉系统拆分开放空间使用权重属性的所有视图。所以这个将尽可能填满你的显示器。在我的案例wrap_content
中,其他视图仍将按照您的需要进行渲染。