【问题标题】:How do I create a customized search-box for my application?如何为我的应用程序创建自定义搜索框?
【发布时间】:2012-04-07 13:03:37
【问题描述】:

如何为我的 Android 应用程序制作自定义搜索框?像这样的东西,例如:

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

【问题讨论】:

    标签: android android-edittext customization search-box


    【解决方案1】:

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

    第一张图片也可以通过使用android:drawableRight属性来实现。 覆盖小部件的 onTouchEvent() 方法可能如下所示:

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP && event.getX() >= getWidth() - getCompoundPaddingRight()) {
            // search drawable was touched
        }
        ...
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2016-07-26
          • 1970-01-01
          • 2020-06-30
          • 1970-01-01
          • 2014-11-11
          • 1970-01-01
          • 2016-12-05
          • 1970-01-01
          • 2021-06-05
          相关资源
          最近更新 更多