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 ();
}
}
更多範例