在安卓应用中将TextView和EditText置于同一行

3
我想在以下的main.xml中把TextView和EditText放在同一行。我该怎么做?
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/txtTotalAmt" android:id="@+id/txtTotalAmt" ></TextView>
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TotalAmt" android:maxLength="6" android:text="0.00" android:inputType="number|numberDecimal"></EditText>

感谢您的帮助。

1个回答

13

我认为一个orientation=horizontal的LinearLayout是你需要的。你也可以尝试使用TableLayout + TableRow。

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" 
            android:layout_height="56px" 
            android:orientation="horizontal">
            <TextView android:layout_height="wrap_content" 
                android:layout_width="wrap_content" 
                android:text="@string/txtTotalAmt" 
                android:id="@+id/txtTotalAmt" >
            </TextView>
            <EditText android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:id="@+id/TotalAmt" 
                android:maxLength="6" 
                android:text="0.00" 
                android:inputType="number|numberDecimal">
            </EditText>
        </LinearLayout>

希望对你有所帮助。


这确实将两个元素放在同一行。谢谢。现在,如果我想要在下一行中添加另一个TextView,应该将它放在另一个LinearLayout中吗? - sherry
谢谢,但我不想使用表格。 - sherry
是的,你可以在水平线性布局周围放置一个<linearLayout android:orientation="vertical"><other text box here></linearLayout>。但是,如果你正在创建更复杂的行(我假设这是一个列表行?),那么最好使用表格行。一个问题是不要过度使用子布局或表格,因为它会消耗资源(http://developer.android.com/resources/tutorials/views/hello-tablelayout.html)。 - wired00
在LinearLayout中拥有多个布局对我很有效。我不想使用表格。不,这不是列表行。只是显示中的部分。我同意。太多的子布局不是一个好主意。我的应用程序很小,我正在为学习目的而做。很高兴知道我不必使用TableLayout。 - sherry
很高兴听到这个 :) 我自己在安卓方面也是新手,所以在这里分享我们学到的东西很好。我只是指出要记住嵌入式表格/布局,以备将来参考。 - wired00

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接