MaterialSearchBar
MaterialSearchBar 0.8.5
Android 的材质设计搜索栏
这个漂亮且易于使用的库将有助于在您的项目中添加 Lollipop Material Design SearchView。
将 SearchBar 添加到您的项目中:
将此代码添加到项目级别的 build.gradle 文件中
allprojects {
repositories {
.. .
maven { url " https://jitpack.io " }
}
}
将依赖项添加到应用程序级别的 build.gradle 文件
dependencies {
implementation ' com.github.mancj:MaterialSearchBar:X.X.X '
}
然后将 SearchBar 添加到您的活动中:
< com .mancj.materialsearchbar.MaterialSearchBar
style = " @style/MaterialSearchBarLight "
app : mt_speechMode = " true "
app : mt_hint = " Custom hint "
app : mt_maxSuggestionsCount = " 10 "
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : id = " @+id/searchBar " />
MaterialSearchBar 具有以下 xml 属性:
属性 | 描述 |
---|---|
mt_语音模式 | 如果设置为 true,将显示麦克风图标而不是搜索图标 |
mt_maxSuggestionsCount | 指定存储的搜索查询的最大数量 |
mt_navIconEnabled | 设置导航图标启用 |
mt_roundedSearchBarEnabled | 在 v21+ 上使用胶囊形状的搜索栏,并在较低版本上恢复为默认值 |
mt_dividerColor | 设置建议和菜单分隔线的颜色 |
mt_searchBarColor | 设置搜索栏的主颜色 |
mt_menuIconDrawable | 设置菜单图标的可绘制性 |
mt_searchIconDrawable | 当语音模式为 false 时设置搜索图标的可绘制性 |
mt_speechIconDrawable | 当语音模式为 true 时设置语音图标的可绘制性 |
mt_backIconDrawable | 设置后退箭头图标的可绘制性 |
mt_clearIconDrawable | 设置透明图标的可绘制性 |
mt_navIconTint | 设置导航/返回动画图标的色调颜色 |
mt_menuIconTint | 设置菜单图标的色调颜色 |
mt_searchIconTint | 设置色调颜色搜索/语音图标 |
mt_backIconTint | 设置后退箭头图标的色调颜色 |
mt_clearIconTint | 设置透明图标的色调颜色 |
mt_navIconUseTint | 动画导航图标是否应该使用色调颜色 |
mt_menuIconUseTint | 菜单图标是否应该使用色调颜色 |
mt_searchIconUseTint | 搜索/语音图标是否应该使用色调颜色 |
mt_backIconUseTint | 后退图标是否应该使用色调颜色 |
mt_clearIconUseTint | 清晰的图标是否应该使用色调颜色 |
mt_提示 | 设置搜索栏聚焦且搜索查询为空时的提示文本 |
mt_占位符 | 设置 MaterialSearchBar 未聚焦时的占位符文本 |
mt_textColor | 设置文本颜色 |
mt_hintColor | 设置提示颜色 |
mt_placeholderColor | 设置占位符颜色 |
mt_textCursorTint | 设置文本光标色调 |
mt_highlightedTextColor | 设置文本突出显示颜色 |
公共方法:
addTextChangeListener(TextWatcher textWatcher)
clearSuggestions()
closeSearch()
openSearch()
getLastSuggestions()
getMenu()
getText()
hideSuggestionList()
inflateMenu(int menuResource)
inflateMenu(int menuResource, int icon)
isSearchOpened()
isSpeechModeEnabled()
isSuggestionsVisible()
setArrowIcon(int arrowIconResId)
setArrowIconTint(int arrowIconTint)
setCardViewElevation(int elevation)
setClearIcon(int clearIconResId)
setClearIconTint(int clearIconTint)
setCustomSuggestionAdapter(SuggestionsAdapter suggestionAdapter)
setDividerColor(int dividerColor)
setHint(CharSequence hintText)
setIconRippleStyle(boolean borderlessRippleEnabled)
setLastSuggestions(List suggestions)
setMaxSuggestionCount(int maxSuggestionsCount)
setMenuDividerEnabled(boolean menuDividerEnabled)
setMenuIcon(int menuIconResId)
setMenuIconTint(int menuIconTint)
setNavButtonEnabled(boolean navButtonEnabled)
setNavIconTint(int navIconTint)
setOnSearchActionListener(OnSearchActionListener onSearchActionListener)
setPlaceHolder(CharSequence placeholder)
setPlaceHolderColor(int placeholderColor)
setRoundedSearchBarEnabled(boolean roundedSearchBarEnabled)
setSearchIcon(int searchIconResId)
setSearchIconTint(int searchIconTint)
setSpeechModeEnabled(boolean speechMode)
setSuggestionsClickListener(SuggestionsAdapter.OnItemViewClickListener listener)
setText(String text)
setTextColor(int textColor)
setTextHighlightColor(int highlightedTextColor)
setTextHintColor(int hintColor)
showSuggestions()
updateLastSuggestions(List suggestions)
样式材质搜索栏
自定义样式 - styles.xml创建自定义样式并使用提供的样式之一作为父样式。
Provided Styles are: MaterialSearchBarLight and MaterialSearchBarDark
Example:
< style name = " MyCustomTheme " parent = " MaterialSearchBarLight " >
< item name = " mt_searchBarColor " >@color/searchBarPrimaryColor</ item >
< item name = " mt_dividerColor " >@color/searchBarDividerColor</ item >
< item name = " mt_navIconTint " >@color/searchBarNavIconTintColor</ item >
< item name = " mt_searchIconTint " >@color/searchBarSearchIconTintColor</ item >
< item name = " mt_clearIconTint " >@color/searchBarClearIconTintColor</ item >
< item name = " mt_menuIconTint " >@color/searchBarMenuIconTintColor</ item >
< item name = " mt_backIconTint " >@color/searchBarBackIconTintColor</ item >
< item name = " mt_textCursorTint " >@color/searchBarCursorColor</ item >
< item name = " mt_textColor " >@color/searchBarTextColor</ item >
< item name = " mt_hintColor " >@color/searchBarHintColor</ item >
< item name = " mt_placeholderColor " >@color/searchBarPlaceholderColor</ item >
< item name = " mt_highlightedTextColor " >@color/searchBarTextHighlightColor</ item >
</ style >
或者
自定义颜色 - color.xml只需设置/更改这些颜色(或某些颜色),您就拥有了自定义样式。
//Material SearchBar Light Theme Colors
< color name = " searchBarIconColor " >#3a3a3a</ color >
//Base
< color name = " searchBarPrimaryColor " >#FFFFFF</ color >
< color name = " searchBarCursorColor " >#8000a1ff</ color >
< color name = " searchBarDividerColor " >#1F000000</ color >
//Icons
< color name = " searchBarNavIconTintColor " >@color/searchBarIconColor</ color >
< color name = " searchBarMenuIconTintColor " >@color/searchBarIconColor</ color >
< color name = " searchBarSearchIconTintColor " >@color/searchBarIconColor</ color >
< color name = " searchBarClearIconTintColor " >@color/searchBarIconColor</ color >
< color name = " searchBarBackIconTintColor " >@color/searchBarIconColor</ color >
//Text
< color name = " searchBarTextColor " >#DE000000</ color >
< color name = " searchBarHintColor " >#42000000</ color >
< color name = " searchBarPlaceholderColor " >#8A000000</ color >
< color name = " searchBarTextHighlightColor " >#8000a1ff</ color >
//Base
< color name = " searchBarPrimaryColorDark " >#303030</ color >
< color name = " searchBarDividerColorDark " >#1FFFFFFF</ color >
//Material SearchBar Dark Theme Colors
< color name = " searchBarIconColorDark " >#00a1ff</ color >
//Icons
< color name = " searchBarNavIconTintColorDark " >@color/searchBarIconColorDark</ color >
< color name = " searchBarMenuIconTintColorDark " >@color/searchBarIconColorDark</ color >
< color name = " searchBarSearchIconTintColorDark " >@color/searchBarIconColorDark</ color >
< color name = " searchBarClearIconTintColorDark " >@color/searchBarIconColorDark</ color >
< color name = " searchBarBackIconTintColorDark " >@color/searchBarIconColorDark</ color >
//Text
< color name = " searchBarTextColorDark " >#DEFFFFFF</ color >
< color name = " searchBarHintColorDark " >#42FFFFFF</ color >
< color name = " searchBarPlaceholderColorDark " >#8AFFFFFF</ color >
< color name = " searchBarTextHighlightColorDark " >#BF00a1ff</ color >
要在活动销毁时保存搜索查询,请使用方法searchBar.getLastSuggestions()
,然后使用searchBar.setLastSuggestions(List<String>);
如下例所示
这是一个使用 MaterialSearchBar 的简单示例
private List < String > lastSearches ;
private MaterialSearchBar searchBar ;
@ Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
searchBar = ( MaterialSearchBar ) findViewById ( R . id . searchBar );
searchBar . setHint ( "Custom hint" );
searchBar . setSpeechMode ( true );
//enable searchbar callbacks
searchBar . setOnSearchActionListener ( this );
//restore last queries from disk
lastSearches = loadSearchSuggestionFromDisk ();
searchBar . setLastSuggestions ( list );
//Inflate menu and setup OnMenuItemClickListener
searchBar . inflateMenu ( R . menu . main );
searchBar . getMenu (). setOnMenuItemClickListener ( this );
}
@ Override
protected void onDestroy () {
super . onDestroy ();
//save last queries to disk
saveSearchSuggestionToDisk ( searchBar . getLastSuggestions ());
}
@ Override
public void onSearchStateChanged ( boolean enabled ) {
String s = enabled ? "enabled" : "disabled" ;
Toast . makeText ( MainActivity . this , "Search " + s , Toast . LENGTH_SHORT ). show ();
}
@ Override
public void onSearchConfirmed ( CharSequence text ) {
startSearch ( text . toString (), true , null , true );
}
@ Override
public void onButtonClicked ( int buttonCode ) {
switch ( buttonCode ){
case MaterialSearchBar . BUTTON_NAVIGATION :
drawer . openDrawer ( Gravity . LEFT );
break ;
case MaterialSearchBar . BUTTON_SPEECH :
openVoiceRecognizer ();
}
}
更多示例