如何在 Android 上正确放置 EditText 和 ImageButton
How to place EditText and ImageButton correctly on Android
我有这样的布局:
<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>
此布局如下所示:
如何更改我当前的代码以便显示图像按钮?谢谢
编辑:
现在看起来像这样:
只需为editText添加
android:layout_width="0dp"
android:layout_weight="1"
试试这个代码:
<?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>
输出将如下所示:
<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>
如果您想将按钮放在右侧或左侧,请将 android:layout_width="fill_parent" 更改为某个值 如果您希望它位于编辑文本的下方或上方,请更改方向
我放置、颜色和图像只是为了做到这一点,用你的替换它应该可以工作:
试试这个:
<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 的值来为您的编辑文本提供更多 space,总数必须等于 1,这就是我放置 0.5 和 0.5
的原因
我有这样的布局:
<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>
此布局如下所示:
如何更改我当前的代码以便显示图像按钮?谢谢
编辑:
现在看起来像这样:
只需为editText添加
android:layout_width="0dp"
android:layout_weight="1"
试试这个代码:
<?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>
输出将如下所示:
<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>
如果您想将按钮放在右侧或左侧,请将 android:layout_width="fill_parent" 更改为某个值 如果您希望它位于编辑文本的下方或上方,请更改方向
我放置、颜色和图像只是为了做到这一点,用你的替换它应该可以工作: 试试这个:
<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 的值来为您的编辑文本提供更多 space,总数必须等于 1,这就是我放置 0.5 和 0.5
的原因