How to place button inside of edit text

2019-01-12 23:44发布

I want to place an image button inside of EditText, but I don't have Idea please tell me how to do so as shown in the figure . Thanks

enter image description here

11条回答
萌系小妹纸
2楼-- · 2019-01-12 23:53

There's solution with clickable compound drawable. It's working very well - I have tested it

SOLUTION

查看更多
祖国的老花朵
3楼-- · 2019-01-12 23:54

Place your EditText inside a linear layout (horizontal) and add a Image Button next to it (0 padding). Setting the background color of EditText and ImageButton will blend in.

查看更多
淡お忘
4楼-- · 2019-01-12 23:58

you can Use the Below Code :

android:drawableRight="@android:drawable/ic_menu_search"

查看更多
Root(大扎)
5楼-- · 2019-01-13 00:06

If You dont want click on that Image, Then you can use drawableRight property for EditText..

android:drawableRight="@drawable/icon"

If you want click then use below code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter search key" />

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/search"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:text="Button"/>

</RelativeLayout>
查看更多
爷的心禁止访问
6楼-- · 2019-01-13 00:06

Use the android:drawableLeft property on the EditText.

<EditText
    ...     
    android:drawableLeft="@drawable/my_icon" />
查看更多
乱世女痞
7楼-- · 2019-01-13 00:07

My solution will fit all screen sizes without the editText going "behind" the imageButton. This also uses android resources, so you do not need to provide your own icon or string resource. Copy and paste this snippet wherever you would like a search bar:

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

   <EditText
        android:layout_margin="8dp"
        android:id="@+id/etSearch"
        android:hint="@android:string/search_go"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/ivSearch"
        android:background="@color/transparent_black"
        android:padding="2dp"
        android:layout_width="wrap_content"
        android:layout_margin="8dp"
        android:src="@android:drawable/ic_menu_search"
        android:layout_height="wrap_content"
        tools:ignore="contentDescription" />

</LinearLayout>

Restult:

enter image description here

查看更多
登录 后发表回答