2017-08-26 141 views
0

我想有這樣的搜索欄...添加搜索工具欄擴展

enter image description here

我只有藍色工具欄沒有搜索

<android.support.v7.widget.Toolbar 
android:id="@+id/toolbar" 
android:title="Messages" 
android:titleTextColor="@color/bg_screen3" 
android:background="@color/bg_screen3" 
android:layout_width="match_parent" 
android:layout_height="112dp" 
android:gravity="center" /> 

如何添加搜索放置在那個位置。?

+0

首先,您從哪裏得到該圖像?一個實際的Android應用程序狀態欄看起來像iPhone ...第二,這絕對是一個自定義可繪製的,而不是一個內置的搜索欄 –

+0

@ cricket_007我改變了圖像... – Ibrahim

回答

0

我建議你可以使用材料搜索查看

<!— Must be last for right layering display —> 
    <FrameLayout 
     android:id="@+id/toolbar_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/theme_primary" /> 

     <com.miguelcatalan.materialsearchview.MaterialSearchView 
      android:id="@+id/search_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </FrameLayout> 

https://github.com/MiguelCatalan/MaterialSearchView

+0

這是一個可擴展的搜索。我需要持續搜索,因爲搜索是應用程序的主要關注點。 – Ibrahim

+0

https://stackoverflow.com/a/36801080/4407266 –

0

您可以通過下面的代碼獲得所需的設計:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/my_toolbar" 
    android:layout_width="wrap_content" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" 
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

<com.arlib.floatingsearchview.FloatingSearchView 
    android:id="@+id/floating_search_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    app:floatingSearch_leftActionMode="showSearch" 
    app:floatingSearch_searchHint="Search..." 
    android:layout_alignParentTop="true" 
    android:layout_alignParentEnd="true" 
    android:layout_marginEnd="10dp" /> 

</android.support.v7.widget.Toolbar> 

</RelativeLayout> 

最後的結局是這樣的。我使用FloatingSearchView庫來獲取持久搜索視圖。

enter image description here

還可以聲明工具欄的高度,使其從默認的高度上延伸。高度爲128dp(Android規格中定義的56dp + 72dp),結果如下: enter image description here