简体   繁体   English

带边框和文本的 SearchView

[英]SearchView with border and text

I want to make my SearchView like this:我想让我的SearchView像这样:
在此处输入图像描述

How can I implement in Java ?如何在Java中实现?

What you see in the image is the Material TextInput with OutLined style.您在图像中看到的是带有OutLined样式的 Material TextInput You can read more about that and the Filled style in the official docs here .您可以在此处的官方文档中阅读有关此内容和Filled样式的更多信息。

Also, don't forget you'll need material library for this.另外,不要忘记为此您需要材料库。 For that, do为此,做

implementation 'com.google.android.material:material:1.2.0-alpha06'

在此处输入图像描述

Now, what you can do with it is to make it like above by adding startDrawable and CornerRadius :现在,你可以用它做的是通过添加startDrawableCornerRadius使它像上面一样:

  1. Create the XML widget as below and put a startIcon .如下创建 XML 小部件并放置一个startIcon

     <com.google.android.material.textfield.TextInputLayout android:id="@+id/editTextLayout" style="@style/TextInputLayoutAppearanceOutLined" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="Search View" app:hintEnabled="true" app:startIconDrawable=" *** Your ICON *** "> <com.google.android.material.textfield.TextInputEditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:textSize="12sp" /> </com.google.android.material.textfield.TextInputLayout>
  2. Then, this is the style TextInputLayoutAppearanceOutLined , put it in your style.xml file:然后,这是样式TextInputLayoutAppearanceOutLined ,将其放入您的 style.xml 文件中:

     <style name="TextInputLayoutAppearanceOutLined" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <item name="hintTextAppearance">@style/HintText</item> <item name="helperTextTextAppearance">@style/HintText</item> <item name="android:textColor">@color/dark_grey</item> <item name="android:textColorHint">@color/colorAccent</item> <item name="hintTextColor">@color/colorAccent</item> <item name="boxStrokeColor">@color/colorAccent</item> <item name="startIconTint">@color/colorAccent</item> <item name="boxBackgroundColor">@color/white</item> <item name="boxCornerRadiusBottomEnd">@dimen/_26sdp</item> <item name="boxCornerRadiusBottomStart">@dimen/_26sdp</item> <item name="boxCornerRadiusTopEnd">@dimen/_26sdp</item> <item name="boxCornerRadiusTopStart">@dimen/_26sdp</item> <item name="boxStrokeWidthFocused">1dp</item> <item name="hintEnabled">true</item> <item name="hintAnimationEnabled">true</item> <item name="colorControlNormal">@color/colorAccent</item> <item name="colorControlActivated">@color/colorPrimary</item>

Obviously, you can set this in XML as well, but style gives you more control over your app to change the element everywhere by one edit.显然,您也可以在 XML 中设置它,但样式可以让您更好地控制您的应用程序,通过一次编辑即可在任何地方更改元素。

  1. Now, you'll have what you want your SearchView to look like, but further to make it act like a SearchView , you need to set filtering for your ListAdapter to make it work.现在,您将拥有您希望SearchView的外观,但要进一步使其像SearchView一样,您需要为ListAdapter设置过滤以使其工作。

For that, you can look here .为此,您可以查看此处

make search view in layout file first首先在布局文件中创建搜索视图

        <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_round"/>

then the make drawable and name it bg_round and put the code below to that drawable然后make drawable并将其命名为bg_round并将下面的代码放入该drawable

<?xml version="1.0" encoding="utf-8"?>
    <shape
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
      <solid android:color="#fff"/> // this the the inner background color
      <stroke android:width="5dp" //this is for the stroke of the border
      android:color="@android:color/black"/> //this is the stroke color
      <corners android:radius="4cdp" /> //this is the corner radius
    </shape>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM