2017-08-26 145 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