34

我有一个两列TableLayout作为滚动视图的唯一子项。第一列包含TextViews('labels'),第二列包含EditText// etc SpinnerDateWidget'values')。即使我已经指定android:layout_width="fill_parent"TableLayout, TableRow& 所有小部件(在“值”列中)。

创建活动时,屏幕看起来很完美。但是,当在 中键入一个非常长的值时EditText,“值”列会超出可见的屏幕区域。

我该如何解决这个问题?

4

6 回答 6

59

您可能希望通过使用权重来定义列的大小。因此,您将定义表格布局高度以填充父级,但对于每一列,您应该将宽度设置为“0px”,并将权重设置为您希望列跨越的百分比。因此,假设您希望第一列是屏幕宽度的 30%,您将其权重设置为“0.3”,将第二列设置为“0.7”。

试试看是否有效。

于 2010-10-30T14:02:23.807 回答
29

解决这个问题的实用方法stretchColumnsshrinkColumns使用TableLayout. 它们的值应该是从 0 开始的列索引。

例如,如果您有一个两列表格布局并且:

  • 您希望第二列填充空白空间(拉伸),以便TableLayout适合大屏幕设备上的整个父视图
  • 您希望在小屏幕设备上缩小第一列(其内容可能被包裹/椭圆化)

你会定义你TableLayout的:

<TableLayout
    android:id="@+id/my_table_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    android:shrinkColumns="0" >
</TableLayout>
于 2013-11-13T16:17:38.580 回答
7

这对我有用..

tableLayout.setColumnShrinkable(1,true);
于 2011-12-29T18:48:30.973 回答
6
<TableLayout 
android:layout_height="fill_parent" 
android:layout_width="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<TableRow 
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView 
android:layout_width="wrap_content"
android:text="Name " 
android:layout_height="fill_parent">
</TextView>
<EditText 
android:layout_width="0dp"
android:layout_weight="1"
android:hint="name"
android:layout_height="wrap_content" 
>
</EditText>
</TableRow>
</TableLayout>

此代码对我有用,可以在第一列上对齐文本视图并编辑文本填充父级。

于 2012-03-28T11:10:13.267 回答
0

我在 Android 2.2 上遇到了与 EditText 类似的问题。就我而言,添加 android:maxWidth="0sp" 属性会有所帮助。现在 EditText 字段显示为我想要的 - 所以它仍然与其他 EditText 字段的大小相同,但是当输入长文本时它不会调整大小。

这是我的 EditText 定义:

<EditText android:id="@+id/extra_edit"
       android:padding="3dip"
       android:inputType="textImeMultiLine"
       android:maxWidth="0sp"
       android:layout_width="fill_parent"
       />
于 2010-11-04T12:59:03.100 回答
0

试试这个,确保android:singleLine="false"android:inputType="textMultiLine"

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:stretchColumns="1">
    <TableRow android:layout_height="wrap_content"
        android:layout_width="fill_parent">

        <TextView android:text="Label:" />
        <EditText android:id="@+id/entry" android:inputType="textMultiLine"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:singleLine="false" />
    </TableRow>
</TableLayout>
于 2010-10-30T14:21:01.560 回答