为什么Android GUI构建如此令人沮丧?我错过了什么?

时间:2013-03-06 01:26:27

标签: android user-interface intellij-idea

我正在尝试为我的Android类项目构建一个GUI。我目前正在使用IntelliJ IDEA 12和Drag-And-Drop GUI Builder。我对文本和编辑属性的了解也足够了。

我遇到的问题是,我似乎无法简单地按照我想要的方式为我的应用程序制作GUI。在普通的Java中,我可以用Swing快速完成。但是,由于Android没有任何Swing,至少可以说是狡猾的。

我认为我可以使用LinearLayout嵌套来完成它。但这一直都是错误的。

下面是我在Netbeans中制作的一个快速图像,说明我希望GUI看起来如何:

enter image description here

我在IntelliJ中根本无法轻易获得。如果我将一个按钮放在水平定向的LinearLayout中,它就可以很好地适应了。但是,如果我再添加一个ImageView,布局就已经搞砸了!然后我在ImageView的另一侧放置了一个按钮,它也没有任何帮助。

我在这里遗漏了什么,或者Android GUI只是令人沮丧? 我可以(像Swing一样)完全用Java编写GUI吗?或者我被迫使用XML?

1 个答案:

答案 0 :(得分:1)

这并不完美,你需要使用它,比如尺寸和定位,但基本上就是你想要的。在Eclipse中花了5分钟,也许更少

<?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:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="105dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_action_search" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.00"
        android:ems="10" />
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        </LinearLayout>
</LinearLayout>