最近我一直在研究我的第一个Android应用程序,它可以转换为不同的温度单位。我已经完成了所有实际编码,但是我遇到了为它创建GUI的问题。我一直在使用DroidDraw UI构建器来生成xml代码;在DroidDraw内部,我在整个UI中使用RelativeLayout。当我在DroidDraw中创建它时,它在预览中看起来很好,但是当我尝试运行应用程序时,它总是看起来像一个混乱的混乱:
这对于不同屏幕尺寸的要求是否有任何要求,如果有,我该如何解决?
添加一些额外的信息,我没有通过模拟器运行它;我在我的captivate上运行它,它有一个480x800的屏幕。此外,我正在使用带有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>
非常感谢任何帮助。
答案 0 :(得分:3)
我之前从未使用droiddraw进行真实世界布局,只是下载它来测试一下 从我看到的:我不建议使用它。
默认情况下,所有生成的布局都使用px
作为单位来指定视图大小。对于Android布局而言,这是一个非常糟糕的想法(由于屏幕尺寸/密度很大)并且几乎不应该是必要的(我还没有找到一个有用的布局。更糟糕的是,我无法在属性选项卡中指定任何其他单位。
这会导致您遇到的确切问题。布局在他们设计的屏幕尺寸上看起来正确,并且在每个其他屏幕上都非常失败。
我建议手工编写XML。或者,如果您更喜欢使用WYSIWYG编辑器,请使用eclipse ADT插件,该编辑器最近进行了大量改进。
答案 1 :(得分:0)
由于Dalvik虚拟机主要基于Java虚拟机而我是Java程序员,因此我可以提供一些建议。如果droid API具有与JPanel等效的内容,请使用Panels组织内容(看起来您可能已经这样做了)。另外,检查框架是否具有“refresh()”类型的功能 - 通常这有助于告诉窗口相应地组织内容。