我在同一水平线上彼此相邻地设置了 EditText Button 。它看起来很棒,除非用户输入大量文本, EditText 的大小调整, Button 被压缩。

我将 EditText Button 设置为 layout_width =" wrap_content" " fill_parent" 弄乱了布局,如果我不需要,我不想使用绝对尺寸 - 它现在看起来在风景和肖像方面都很棒,我只是不喜欢不希望 EditText 调整大小。

我的布局:

<TableLayout
    android:id="@+id/homelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow>
        <TextView
            android:id="@+id/labelartist"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Find artists:" />
    </TableRow>
    <TableRow>
        <EditText
            android:id="@+id/entryartist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:background="@android:drawable/editbox_background"
            android:editable="true"
            android:padding="5px"
            android:singleLine="true" />
        <Button
            android:id="@+id/okartist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dip"
            android:layout_weight="1"
            android:text="Search" />
    </TableRow>

</TableLayout>
有帮助吗?

解决方案

对于EditText,请使用

android:layout_width="0dp" - not required but preferred.
android:layout_weight="1"

对于Button,不要指定 android:layout_weight

其他提示

我所做的是在活动的onCreate中,首先测量EditText,然后应用其maxWidth。

您可以使用类似于以下内容的代码执行此操作:

EditText someedittext = (EditText)findViewById(R.id.sometextview);
someedittext.setMaxWidth(someedittext.getWidth());

EditText 一个 maxWidth 。这应该阻止它调整超出你提供的宽度。此外,如果您希望它包含在1行内,请将 maxLines 设置为1。

只需使用:

android:layout_width="fill_parent"

用于EditText和Button。它也可以为EditText设置,但最好同时使用它。

您需要添加android:imeOptions以防止此行为。

点击此处

您可以定义上面显示的edittext的大小

android:minWidth="80dp"
android:maxWidth="80dp"

android:layout_width="60dp"

您可以应用所需尺寸的背景图像 但不要忘记定义它的高度和宽度 with wrap_content。

  android:layout_width="wrap_content"
  android:layout_height="wrap_content"

我有类似的问题,但我无法完成上述工作。我所做的是从EditText框中获取文本,然后将其格式化为我的屏幕可以处理的最大大小。当我将其保存到数据库时,我将其保存为显示文本和原始文本。

如果你去到这个页面,我会做一个更长的解释。

执行此操作的正确方法是将android:minWidth和android:maxWidth设置为您想要的宽度。这样您就不必调整布局来使用android:layout_weight。

例如,如果您希望EditText始终为80个密度像素,无论您键入多少个字符,请使用以下内容:

android:minWidth="80dp"
android:maxWidth="80dp"

我有确切的查询。但我在线性布局中有一个EditText和一个按钮。 我试过 android:layout_weight =&quot; 1&quot;对于EditText         机器人:layout_width = QUOT; 0dp&QUOT; 它工作得很完美! 非常感谢!

选择的解决方案有效,但让我补充一点:
如果你使用 android:layout_weight =&quot; 0.15&quot; (值不重要)
然后点击 如果 LinearLayout 是水平的,则 android:layout_width =&quot; 0dp&quot;
或点击 android:layout_height =&quot; 0dp&quot; 如果 LinearLayout 是垂直的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top