Android GUI - 如何强制按钮到下一行?

TAT*_*ATN 6 layout user-interface android

对不起,如果我问这样一个基本问题.我整个上午都试图谷歌,并阅读developer.android.com上的文档,但我找不到解决方案.

简而言之,我想创建一个如下所示的界面:

在此输入图像描述

这是我到目前为止:

在此输入图像描述

现在,我正在尝试将2个按钮带到下一行,但我仍然没有运气.如果我将android:orientation ="horizo​​ntal"更改为"vertical",则所有内容都垂直对齐.我试图在Button标签中加入android:orientation ="vertical"行,但这似乎不起作用.

我还能尝试什么?有人会给我一些指示吗?(我是Android开发的新手,也是所有这些XML的东西).


下面是我的XML代码:

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

<EditText android:id="@+id/txt_Input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:hint="@string/txt_Input"

        android:layout_weight="4"                      
/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_Send" 

        android:layout_weight="1"

        android:onClick="sendMessage"
/>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_Send" 

        android:layout_weight="1"

        android:onClick="sendMessage"
/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Cas*_*eyB 6

您拥有LinearLayout中的所有项目,其方向设置为"水平",因此它会水平排列所有UI组件.您需要做什么使用RelativeLayout并将按钮设置为EditText下面的布局.

  • 或者,将方向设置为垂直,并使用嵌套的水平线性布局将按钮括在edittext下方. (2认同)
  • @withoutclass:效率低于相对布局 (2认同)

meh*_*meh 5

只需使用RelativeLayout,您的按钮应如下所示:

<Button
    android:id="@+id/send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_Send" 
    android:onClick="sendMessage"
    android:layout_below="@+id/txt_Input"
    android:layout_alignParentLeft="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_Send" 
    android:onClick="sendMessage"
    android:layout_below="@+id/txt_Input"
    android:layout_toRightOf="@+id/send"/>
Run Code Online (Sandbox Code Playgroud)