代码之家  ›  专栏  ›  技术社区  ›  Manfred Moser

表格布局列大小调整

  •  1
  • Manfred Moser  · 技术社区  · 15 年前

    我有一个表布局,其中有三列表示一个窗体

    需要的符号标签输入字段。

    我希望输入字段填充标签右侧屏幕的剩余宽度。到目前为止,我找到的唯一解决办法是

    <TableLayout android:layout_height="wrap_content"
           android:layout_width="fill_parent" 
           android:stretchColumns="2">
        <TableRow>
                        <TextView android:text="@string/requiredSymbol"
                                  />
         <TextView android:text="@string/Name"
                                  android:textColor="@color/orange"
                                  android:gravity="left|center_vertical"
             />
         <EditText android:id="@+id/contactName"
             android:inputType="text|textPersonName"
                                  android:ellipsize="end"
                                android:singleLine="true"
                                android:scrollHorizontally="true"
                                android:maxWidth="1sp"
                                />
        </TableRow>
    

    maxwidth需要有一些值,实际上被忽略了。然后StretchColumns值可以很好地完成它的工作。请注意,如果edittext的内容使表跨过屏幕边框(imho a bug…),这也可以工作。

    这现在很好用,但在我看来有点像黑客。有更好的方法吗?

    4 回复  |  直到 13 年前
        1
  •  2
  •   Rich    14 年前

    尝试在列上使用android:layout_weight,但确保在TableLayout中设置android:StretchColumns。我在TableLayout中遇到了一些问题,片面地决定缩小列,从而导致不需要的文本包装。我不确定布局重量和单侧柱收缩的使用是否相关。

        2
  •  0
  •   gtcompscientist    14 年前

    我实现这一目标的方法是 width EditText 添加到具有宽度的布局后的字段 FILL_PARENT 然后用它来设置 MaxWidth .

    结果是这样的:

    EditText exampleText = new EditText(this);
    exampleText.setText([Your Text]);
    
    //This says to fill width, but wrap content for the height
    //So it should expand to whatever you've set the MaxLines to be...
    LayoutParams exampleLP = new LayoutParams(FILL_PARENT, WRAP_CONTENT);
    [Parent].addView(exampleText, exampleLP);
    
    //Here is where it's actually setting the width.
    exampleText.setMaxWidth(exampleText.getWidth());
    

    不是我最喜欢的方式,但绝对可靠。

        3
  •  0
  •   Manfred Moser    14 年前

    似乎在这个阶段没有更好的方法来使用静态XML文件。但是,我后来更改了布局,使其不具有所需标签/图标的单独列,并将其更改为使用EditText SetError消息以及每个字段上的TextWatcher实现,这无论如何都要好得多。

        4
  •  0
  •   Matt    13 年前

    我可以通过将EditText包装在框架布局中并将EditText设置为与父级匹配,从而使EditText与剩余宽度匹配。这似乎也适用于纺纱厂。

    <TableLayout
        android:shrinkColumns="1"
        android:stretchColumns="1" >
    
        <TableRow>
    
            <!-- column 0 -->
            <TextView
                android:text="some text" />
    
            <!-- column 1 -->
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    
            </FrameLayout>
    
        </TableRow>
    
    </TableLayout>