如何在Android上正确放置EditText和ImageButton

3
我有这个布局:
  <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <EditText
                android:layout_width="0dp"
                android:layout_weight="1"
                android:imeOptions="actionSearch"
                android:inputType="text"
                android:gravity="center"

                android:layout_height="wrap_content"
                android:id="@+id/et_word"
                android:drawableBottom="@color/bottomcolor"/>

            <!--android:ems="14" -->

            <ImageButton
                android:id="@+id/btn_getir"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:src="@drawable/search"
                android:background="@color/white"
                android:paddingRight="0dp"
                android:paddingLeft="0dp"
                android:paddingTop="0dp"
                android:paddingBottom="15dp"
                />

        </LinearLayout>

这个布局看起来像这样: enter image description here 如何修改当前代码以显示图像按钮?谢谢。
编辑: 现在它看起来像这样:enter image description here

您想将按钮放置在哪里? - fandro
@fandro 在右侧的EditText旁边。 - jason
editText的宽度不应该是fill_parent,而应该是wrap_content。 - Opiatefuchs
这就是你想要的,对吗? - Iulian Popescu
@IulianPopescu 不,这不是我想要的,也不正确。EditText没有填满所有剩余的空间...右边还有空间。 - jason
显示剩余6条评论
5个回答

3

只需在输入框中添加

android:layout_width="0dp"
android:layout_weight="1"

我按照你说的做了,但是EditText没有填满所有剩余的空间。 - jason
好的,应该可以。你能更新一下 XML 和结果吗? - Iulian Popescu

3
 <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <EditText
            android:imeOptions="actionSearch"
            android:inputType="text"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/et_word"
            android:drawableBottom="@color/bottomcolor"/>

        <!--android:ems="14" -->

        <ImageButton
            android:id="@+id/btn_getir"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:src="@drawable/search"
            android:background="@color/white"
            android:paddingRight="0dp"
            android:paddingLeft="0dp"
            android:paddingTop="0dp"
            android:paddingBottom="15dp"
            />

    </LinearLayout>

2

试试这段代码:

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

  <LinearLayout 
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="@color/white"
           android:orientation="vertical">

           <LinearLayout
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:orientation="horizontal"
               android:paddingLeft="10dp"
               android:paddingRight="10dp">

               <EditText
                   android:layout_weight="1"
                   android:imeOptions="actionSearch"
                   android:inputType="text"
                   android:gravity="center"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:id="@+id/et_word"
                   android:drawableBottom="@color/bottomcolor"/>

               <!--android:ems="14" -->

               <ImageButton
                   android:id="@+id/btn_getir"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:src="@drawable/pink"
                   android:background="@color/white"
                   android:paddingRight="0dp"
                   android:paddingLeft="0dp"
                   android:paddingTop="0dp"
                   android:paddingBottom="15dp"
                   />

           </LinearLayout>


       </LinearLayout>

输出结果将如下所示:this

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Opiatefuchs
@Opiatefuchs 这个正常工作了..我已经添加了输出屏幕截图。你可以看一下。 - User_1191

2

我只是随便放置了颜色和图像,为了替换成你自己的内容,尝试以下代码:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="0.5">

        <EditText
            android:imeOptions="actionSearch"
            android:inputType="text"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_word"
            android:drawableBottom="@color/colorAccent"/>

            </LinearLayout>
    <!--android:ems="14" -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="0.5">

        <ImageButton
            android:id="@+id/btn_getir"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:src="@android:drawable/btn_star"
            android:background="@color/colorAccent"
            android:paddingRight="0dp"
            android:paddingLeft="0dp"
            android:paddingTop="0dp"
            android:paddingBottom="15dp"
            />
    </LinearLayout>
</LinearLayout>

编辑: 您可以通过调整layout_weight的值来给您的编辑文本框更多的空间,总和必须等于1,这就是为什么我放置了0.5和0.5。


0

如果您想将按钮放置在右侧或左侧,可以将android:layout_width="fill_parent"更改为某个值;如果您想将其放置在编辑文本下方或上方,请更改方向。


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