【问题标题】:Android GUI - How to force a button to the next line?Android GUI - 如何强制按钮到下一行?
【发布时间】:2012-10-13 20:36:40
【问题描述】:

抱歉,我问的是这样一个基本问题。我整个早上都在尝试谷歌,并阅读了 developer.android.com 上的文档,但我找不到解决方案。

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

这是我目前所拥有的:

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

我还能尝试什么?有人能给我一些指点吗? (我是 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>

【问题讨论】:

    标签: android user-interface layout


    【解决方案1】:

    LinearLayout 中的所有项目都将其方向设置为水平,因此它水平排列所有 UI 组件。你需要做的是使用RelativeLayout并将按钮设置为EditText下方的布局。

    【讨论】:

    • 或者,将方向设置为垂直并使用嵌套的水平线性布局将按钮包围在编辑文本下方。
    • @withoutclass: 这会比相对布局效率低
    • 应该差别不大。
    • 谢谢你,凯西。现在我知道还有另一种布局。
    • @Asahi 是的,它的效率会稍低一些,并且会导致更丑陋的布局代码,但它可以作为一种解决方案。这就是为什么我没有将其发布为答案。
    【解决方案2】:

    只需使用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"/>
    

    【讨论】:

    • 感谢您的详细解答。这很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 2015-08-05
    • 2019-07-12
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多