Android 如何为我的应用程序创建自定义搜索框?

Android 如何为我的应用程序创建自定义搜索框?,android,android-edittext,customization,search-box,Android,Android Edittext,Customization,Search Box,如何为我的android应用程序创建自定义搜索框?例如,类似这样的事情: 或者如果很难,是否可以在本地应用程序中使用AndroidSearchManager 只需使用您自己的图像作为背景,而不是默认图像 对于EditText视图,我建议查看它,以便能够在任何屏幕上平滑地调整大小。只需使用您自己的图像作为背景,而不是默认图像 对于EditText视图,我建议查看它,以便能够在任何屏幕上平滑地调整大小。正如bughi所述,为您的小部件使用自定义的背景9补丁绘图。 第二个图像由并排放置的EditTe

如何为我的android应用程序创建自定义搜索框?例如,类似这样的事情:

或者如果很难,是否可以在本地应用程序中使用Android
SearchManager


只需使用您自己的图像作为背景,而不是默认图像


对于
EditText
视图,我建议查看它,以便能够在任何屏幕上平滑地调整大小。

只需使用您自己的图像作为背景,而不是默认图像


对于
EditText
视图,我建议查看它,以便能够在任何屏幕上平滑地调整大小。

正如bughi所述,为您的小部件使用自定义的背景9补丁绘图。 第二个图像由并排放置的EditText和ImageButton组成。 对EditText使用这样的9-patch可绘制,对ImageButton使用这样的9-patch可绘制。 确保将选择器用作android:background,将小部件状态设置为正常、按下并聚焦

也可以使用属性
android:drawableRight
获得第一张图像。 小部件的重写方法可以如下所示:

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP && event.getX() >= getWidth() - getCompoundPaddingRight()) {
        // search drawable was touched
    }
    ...
}
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="10dp" />

    <solid android:color="@android:color/white" />

    <padding 
        android:left="8dp"
        android:right="8dp"
        android:top="8dp"
        android:bottom="8dp" />

</shape>

正如bughi所说的,为您的小部件使用定制的背景9-patch绘图。 第二个图像由并排放置的EditText和ImageButton组成。 对EditText使用这样的9-patch可绘制,对ImageButton使用这样的9-patch可绘制。 确保将选择器用作android:background,将小部件状态设置为正常、按下并聚焦

也可以使用属性
android:drawableRight
获得第一张图像。 小部件的重写方法可以如下所示:

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP && event.getX() >= getWidth() - getCompoundPaddingRight()) {
        // search drawable was touched
    }
    ...
}
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="10dp" />

    <solid android:color="@android:color/white" />

    <padding 
        android:left="8dp"
        android:right="8dp"
        android:top="8dp"
        android:bottom="8dp" />

</shape>

对于背景,我认为使用如下可绘制形状更容易:

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP && event.getX() >= getWidth() - getCompoundPaddingRight()) {
        // search drawable was touched
    }
    ...
}
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="10dp" />

    <solid android:color="@android:color/white" />

    <padding 
        android:left="8dp"
        android:right="8dp"
        android:top="8dp"
        android:bottom="8dp" />

</shape>


半径为背景提供了圆形边框,并且可以轻松调整,改变填充的大小也是如此。

对于背景,我认为使用这样的可绘制形状更容易:

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP && event.getX() >= getWidth() - getCompoundPaddingRight()) {
        // search drawable was touched
    }
    ...
}
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="10dp" />

    <solid android:color="@android:color/white" />

    <padding 
        android:left="8dp"
        android:right="8dp"
        android:top="8dp"
        android:bottom="8dp" />

</shape>

半径为背景提供了圆角边框,并且可以轻松调整,改变填充的大小也是如此