应用Edittext实现搜索框的功能

来源:互联网 发布:编程语言排名 编辑:程序博客网 时间:2024/05/18 01:27

因为产品部的要求,希望将搜索框改版一下,具体要求是提示图标和提示文字都要居于Edittext下方。关键是设置了gravity属性之后,文本可以居于图标下方。但是运用android:drawableLeft属性设置图标时图标仍然继续居中,各种捣鼓无果。

    于是转换思路,不应用drawableLeft属性,而是想文本框左边添加ImageButton,设置layout_gravity属性,一击即中.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    <LinearLayout
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_below="@id/title"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="horizontal" >
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:src="@drawable/search"
            android:layout_gravity="bottom"
/>
        <EditText
            android:id="@+id/edit_text"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="@null"
            android:gravity="bottom"
            android:hint="提示框"
            android:singleLine="true"
            android:textColor="@android:color/black"
            android:textColorHint="#CCCCCE"
            android:textCursorDrawable="@null"
            android:textSize="@dimen/H3" />
        <ImageButton
            android:id="@+id/btn_clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:src="@drawable/comm_ad_close"
            android:layout_gravity="bottom"
            android:visibility="gone" />
    </LinearLayout>

    效果图如下:

    如果有其他方法实现这种效果,欢迎赐教.......

0 0