Android - UI元素超出屏幕

6

我在定位布局元素时遇到了问题。我的TableLayout中的AutoComplete和其后的按钮使得TableRow扩展到了屏幕宽度之外。有人知道为什么吗?以下是我的XML代码以及问题的图片。

提前感谢!

<?xml version="1.0" encoding="utf-8"?>

<TableRow android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <AutoCompleteTextView android:id="@+id/installation"
        android:textSize="14sp" android:completionThreshold="3" />
</TableRow>
<TableRow android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button android:id="@+id/btn_find" android:text="@string/btn_find"></Button>
</TableRow>
<TableRow android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/error" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:paddingTop="20px"
        android:textColor="#FF0000" />
</TableRow>

Picture of UI


在实际设备上也是这样的吗? - Robby Pond
2个回答

39

对于确实需要表格布局的情况,可以使用layout_weight选项。见下方代码:

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="6sp" android:id="@+id/detailsLayout">
            <TableRow
                android:layout_width="wrap_content"
                android:id="@+id/TableRow03"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/TextView06"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="Detail 1"></TextView>
                <TextView
                    android:text="@string/empty_value"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:id="@+id/sessionAudience"
                    android:layout_weight="1"></TextView>
            </TableRow>
         </TableLayout>

5
默认情况下,TableRow 中的小部件具有 android:layout_width="wrap_content"。这并不限制您的屏幕大小 - wrap_content 的意思是“包裹内容”,而不是“包裹内容,但请不要超出屏幕边缘,如果您不介意的话”。
在这种情况下,您不需要使用 TableLayout 和一组 TableRows,因为您没有表格。
因此,一种方法是使用 RelativeLayout 并使您的 AutoCompleteTextView 使用 android:layout_alignParentLeft="true"android:layout_alignParentRight="true"。这将将其固定到 RelativeLayout 的外部边缘,您可以使用 android:layout_width=fill_parent 调整其大小以填充屏幕。

抱歉,表格代码已被删除。我定义了一个TableLayout,方向设置为垂直,并且layout_width和layout_height都设置为fill_parent。有什么想法吗? - Ryan
1
我的想法在上面。如果您不采取任何措施告诉AutoCompleteTextView保持在屏幕范围内,它就不会保持在屏幕范围内。一种实现这一目的的方法是使用如我上面所述的RelativeLayout。 - CommonsWare
没事了,这帮了很多!看起来已经成功了 :) 谢谢!! - Ryan

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