【问题标题】:Android App GUI安卓应用程序图形用户界面
【发布时间】:2011-12-21 00:40:52
【问题描述】:

最近,我一直在开发我的第一个 android 应用程序,该应用程序可以在不同的温度单位之间进行转换。我已经完成了所有实际的编码,但我在为其创建 GUI 时遇到了问题。我一直在使用 DroidDraw UI builder 来生成 xml 代码;在 DroidDraw 内部,我在整个 UI 中都使用了 RelativeLayout。当我在 DroidDraw 中创建它时,它在预览中看起来不错,但是当我尝试运行该应用程序时,它总是看起来基本上是一团糟:

这是否与不同屏幕尺寸的要求有关,如果是,我该如何解决?

添加一些额外的信息,我不是通过模拟器运行它;我在具有 480x800 屏幕的 captivate 上运行它。此外,我正在使用带有 NBAndroid 插件的 NetBeans IDE 进行构建。

这是我的 [main.xml][2] 文件:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:id="@+id/widget32"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <EditText
        android:id="@+id/input"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_alignTop="@+id/output"
        android:layout_alignParentLeft="true"
        >
    </EditText>
    <TextView
        android:id="@+id/widget42"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Output"
        android:layout_below="@+id/ftok"
        android:layout_alignLeft="@+id/ktoc"
        >
    </TextView>
    <TextView
        android:id="@+id/widget41"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Input"
        android:layout_below="@+id/ftok"
        android:layout_alignParentLeft="true"
        >
    </TextView>
    <Button
        android:id="@+id/ftok"
        android:layout_width="104px"
        android:layout_height="wrap_content"
        android:text="Far To Kel"
        android:textStyle="bold"
        android:layout_alignTop="@+id/ctok"
        android:layout_alignParentLeft="true"
        >
    </Button>
    <Button
        android:id="@+id/ctok"
        android:layout_width="104px"
        android:layout_height="wrap_content"
        android:text="Cel To Kel"
        android:textStyle="bold"
        android:layout_alignTop="@+id/ktoc"
        android:layout_alignLeft="@+id/ctof"
        >
    </Button>
    <Button
        android:id="@+id/ktoc"
        android:layout_width="104px"
        android:layout_height="wrap_content"
        android:text="Kel To Cel"
        android:textStyle="bold"
        android:layout_below="@+id/ctof"
        android:layout_alignParentRight="true"
        >
    </Button>
    <Button
        android:id="@+id/ctof"
        android:layout_width="104px"
        android:layout_height="wrap_content"
        android:text="Cel To Far"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        >
    </Button>
    <Button
        android:id="@+id/ktof"
        android:layout_width="104px"
        android:layout_height="wrap_content"
        android:text="Kel To Far"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        >
    </Button>
    <Button
        android:id="@+id/ftoc"
        android:layout_width="104px"
        android:layout_height="wrap_content"
        android:text="Far To Cel"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        >
    </Button>
    <TextView
        android:id="@+id/widget50"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Temperature Conversion is Fun!"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_below="@+id/widget49"
        android:layout_toRightOf="@+id/widget41"
        >
    </TextView>
    <TextView
        android:id="@+id/widget49"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Kel=Kelvin"
        android:layout_below="@+id/widget48"
        android:layout_alignLeft="@+id/ctok"
        >
    </TextView>
    <TextView
        android:id="@+id/widget48"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cel=Celcius"
        android:layout_below="@+id/widget47"
        android:layout_alignLeft="@+id/ctok"
        >
    </TextView>
    <TextView
        android:id="@+id/widget47"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Far=Fahrenheit"
        android:layout_below="@+id/input"
        android:layout_alignLeft="@+id/ctok"
        >
    </TextView>
    <EditText
        android:id="@+id/output"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_below="@+id/widget42"
        android:layout_toRightOf="@+id/ctok"
        >
    </EditText>
</RelativeLayout>

任何帮助将不胜感激。

【问题讨论】:

    标签: android xml user-interface android-relativelayout


    【解决方案1】:

    我以前从未将 droiddraw 用于现实世界的布局,只是下载了它来测试一下。
    从我所见:我不建议使用它。

    所有生成的布局默认使用px作为一个单位来指定视图大小。就 android 布局而言,这是一个非常糟糕的想法(由于周围有许多屏幕尺寸/密度),几乎永远不需要(我还没有找到一个有用的布局)。更糟糕的是,我无法在属性选项卡中指定任何其他单位。

    这会导致您看到的确切问题。布局在他们设计的屏幕尺寸上看起来正确,但在其他所有屏幕上都严重失败。

    我建议手动编写 XML。或者,如果您更喜欢使用所见即所得的编辑器,请使用 eclipse ADT 插件,该编辑器最近改进了很多。

    【讨论】:

    • +1 使用px 几乎肯定是问题所在。如果这是 DroidDraw 的默认设置,那就是设计缺陷。
    • 这不仅是默认值,我根本无法使用dp,在这种情况下会引发错误。听说有人认为这个工具很有用,我想知道我是否遗漏了什么。
    • @alextsc 感谢您的回复,我会试一试eclipse插件。
    【解决方案2】:

    由于 Dalvik 虚拟机很大程度上基于 Java 虚拟机,而我是一名 Java 程序员,我可以提供一些建议。如果 droid API 具有与 JPanel 等效的功能,请使用 Panels 组织内容(看起来您可能已经这样做了)。此外,检查框架是否具有“refresh()”类型的函数 - 通常这有助于告诉窗口相应地组织内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多