ANDROID布局从模拟器变为平板电脑

时间:2011-03-01 15:43:50

标签: android eclipse android-layout tablet

我最近使用Eclipse开发了一款安卓游戏。我真的是新手,请原谅我,如果我要问的话太简单了。 >。<

应用程序在Eclipse中安装的Android模拟器中运行并完美呈现。但是,当我们尝试将它安装在三星Galaxy选项卡中时,它显然比模拟器有更大的屏幕,布局变得混乱。按钮不是正确的顺序等。我确实有使用XML布局的屏幕,有些是带有精灵的简单画布。知道如何将模拟器中的原始布局保留到平板电脑吗?我尝试使用RelativeLayout代替LinearLayout,但它仍无效。

任何帮助将不胜感激。谢谢!

一个XML文件代码,它是来自仿真器的相应屏幕,如下所示。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/MenuLayout" >

<Button android:id="@+id/btnNew"
    android:layout_width="140px"
    android:layout_height="65px"
    android:layout_marginTop="160px"
    android:background="@drawable/newgame"/>
<Button android:id="@+id/btnInst"
    android:layout_width="140px"
    android:layout_height="65px"
    android:background="@drawable/instructions" />
<Button android:id="@+id/btnCredits"
    android:layout_width="140px"
    android:layout_height="65px"
    android:background="@drawable/credits"/>
<Button android:id="@+id/btnExit"
    android:layout_width="140px"
    android:layout_height="65px"
    android:background="@drawable/exit"/> </LinearLayout>

corresponding image for xml above

4 个答案:

答案 0 :(得分:0)

您需要使用dpi而不是像素。 这是一个很好的资源。 http://developer.android.com/guide/practices/screens_support.html

答案 1 :(得分:0)

正如其他人所提到的,你应该使用“dp”而不是“px”。这些是与设备无关的像素,可以缩放以匹配不同的屏幕分辨率。

您还应该遵循此guide了解如何使用文件夹结构来支持不同的屏幕分辨率和尺寸。

干杯

答案 2 :(得分:0)

根据您编辑过的问题和评论,以下是将布局更改为相对布局的方法:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     style="@style/MenuLayout"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

    <Button android:id="@+id/btnNew"
        android:layout_width="140dip"
        android:layout_height="65dip"
        android:layout_marginTop="160dip"
        android:background="@drawable/newgame" 
        android:layout_centerHorizontal="true" 
        android:layout_above="@+id/btnInst" />

    <Button android:id="@+id/btnInst"
        android:layout_width="140dip"
        android:layout_height="65dip"
        android:background="@drawable/instructions" 
        android:layout_centerHorizontal="true" 
        android:layout_above="@+id/btnCredits"/>

    <Button android:id="@+id/btnCredits"
        android:layout_width="140dip"
        android:layout_height="65dip"
        android:background="@drawable/credits"
        android:layout_centerHorizontal="true" 
        android:layout_above="@+id/btnExit"/>

    <Button android:id="@+id/btnExit"
        android:layout_width="140dip"
        android:layout_height="65dip"
        android:background="@drawable/exit"
        android:layout_centerHorizontal="true" 
        android:layout_alignParentBottom="true" 
        android:layout_marginBottom="25dip"/> 

</RelativeLayout>

但是,如果你想要一个完美的解决方案,你需要设计3种尺寸的按钮和背景图片,并将它们放在各自的文件夹drawable-hdpi,drawable-mdpi和drawable-ldpi(以及用于平板电脑,来自2.2) ,你也可以有drawable-xhdpi!)。仔细阅读此article并在模拟器中测试您的布局尽可能多的屏幕(来自ADT 10.0,直接从xml中的Graphical Layout选项卡预览不同配置的布局非常容易。

此外,android:layout_marginTop="160dip"android:layout_marginBottom="25dip"在相对布局中并不真正有用。我建议你尝试9-patch picture为你的背景,只有木板可伸缩按钮(虽然我不确定它的工作方式如此)。或者,我会有一个单独的图片与主菜单,我将其放在按钮上方,并在背景中仅保留树木和其他所有内容。

答案 3 :(得分:0)

最好为不同的屏幕尺寸提供不同的布局。在开发游戏时我也有同样的问题。完成它。 你可能已经提供了布局,所以你只是提供 布局 - 大,小,和xlarge.and反对你的布局你必须提供可绘制的文件夹,如hdpi,mdpi,ldpi,xdpi文件夹,你必须保存不同大小的图像58 * 58,75 * 75,100 * 100以便随后你检查它你会得到它。

相关问题