应用Edittext实现搜索框的功能

本文介绍了一种解决Android搜索框布局问题的方法,即通过在文本框左侧添加ImageButton并设置layout_gravity属性,使提示图标和提示文字能够居于EditText下方。提供了示例代码和布局结构,供遇到类似需求的开发者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

    <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>

    效果图如下:

102502_ktXk_1174477.png

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

转载于:https://my.oschina.net/weiCloudS/blog/291826

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值