工具栏中的Android持久搜索栏

时间:2016-01-28 17:41:49

标签: android material-design persistent searchbar

我是Android开发的新手。我浏览了Google Udacity课程,目前正在尝试编写应用程序代码。具体来说,我试图将我当前的应用程序(用QT编写)切换到原生Android。

我想做的是做类似的事情:

http://i.stack.imgur.com/Exm2y.png

这是从用户体验堆栈交换的Material Design - Persistent search, with navigation drawer问题中获取的。

我可以做顶部,但我无法弄清楚如何在底部获得持久搜索栏。我一直在尝试不同的解决方案(活动栏和工具栏的自定义主题),但是无法远程关闭。有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

只需要将所有内容放在AppBarLayout中。工具栏和任何用于持久搜索的库。 Here非常有用,所以最后为AppBarLayout设置layout_height。像这样的东西

<android.support.design.widget.AppBarLayout
    android:id="@id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/margin_130"
    android:background="@drawable/bg_tab_degraded"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <android.support.v7.widget.Toolbar
        android:id="@id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@drawable/bg_tab_degraded"
        app:layout_scrollFlags="scroll|enterAlways"
        />

    <com.arlib.floatingsearchview.FloatingSearchView
        android:id="@+id/floating_search_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_tab_degraded"
        app:floatingSearch_searchBarMarginLeft="@dimen/margin_20"
        app:floatingSearch_searchBarMarginTop="@dimen/margin_5"
        app:floatingSearch_searchBarMarginRight="@dimen/margin_20"
        app:floatingSearch_searchHint="Search..."
        app:floatingSearch_suggestionsListAnimDuration="250"
        app:floatingSearch_showSearchKey="false"
        app:floatingSearch_leftActionMode="showSearch"
        app:floatingSearch_menu="@menu/main"
        app:floatingSearch_close_search_on_keyboard_dismiss="true"/>



</android.support.design.widget.AppBarLayout>

我希望,它会对你有所帮助。祝你好运。

答案 1 :(得分:0)

您可以尝试PersistentSearch

search = (SearchBox) findViewById(R.id.searchbox);
for(int x = 0; x < 10; x++){
    SearchResult option = new SearchResult("Result " + Integer.toString(x), getResources().getDrawable(R.drawable.ic_history));
    search.addSearchable(option);
}       
search.setLogoText("My App");
search.setMenuListener(new MenuListener(){

        @Override
        public void onMenuClick() {
            //Hamburger has been clicked
            Toast.makeText(MainActivity.this, "Menu click", Toast.LENGTH_LONG).show();              
        }

    });
search.setSearchListener(new SearchListener(){

    @Override
    public void onSearchOpened() {
        //Use this to tint the screen
    }

    @Override
    public void onSearchClosed() {
        //Use this to un-tint the screen
    }

    @Override
    public void onSearchTermChanged() {
        //React to the search term changing
        //Called after it has updated results
    }

    @Override
    public void onSearch(String searchTerm) {
        Toast.makeText(MainActivity.this, searchTerm +" Searched", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onResultClick(SearchResult result){
        //React to a result being clicked
    }


    @Override
    public void onSearchCleared() {

    }

});

同时检查https://github.com/UsherBaby/SearchView-1

enter image description here

答案 2 :(得分:0)

您可以尝试这个。我已经使用了这个github库

https://github.com/arimorty/floatingsearchview