2016-05-29 123 views
8

我试图添加一个简单的SearchView到我的应用程序中的工具栏。它的所有工作都很好地期望布局。工具栏中的左侧空间与SearchView android studio

NavigationDrawer图标和SearchView之间有这个“空格”。 如果我设置了标题,则该空格将填充该字符串,否则为空。

空字符串:

enter image description here

随着标题字符串:

enter image description here

如何消除恼人的空白?

编辑:

我使用作为基码的Android从Studio中NavigationDrawer示例

main.xml中

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainActivity" 
    > 

    <item 
     android:id="@+id/action_search" 
     android:icon="@android:drawable/ic_menu_search" 
     android:title="Search" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:showAsAction="always" /> 

    </menu> 

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="ga.musicforyou.musicforyou.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      app:contentInsetEnd="0dp" 
      app:contentInsetStart="0dp" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      /> 

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


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

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:itemTextColor="#FFF" 
     app:itemIconTint="#FFF" 

     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 
+0

你可以显示布局代码吗? – slanecek

+0

是的,我编辑了我的问题 –

回答

6

工具栏从FrameLayout里延伸,你可以把意见里面。像这样:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/toolbar" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@color/primary_color"> 

<SearchView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

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

编辑:你把它放在布局中作为菜单。它始终位于工具栏的右侧,因为它是一个菜单。如果你想更直接地控制工具栏中的视图,只要把它放在那里,就像我向你展示的那样。

+0

Definetly是正确的答案!你能帮助下一步吗?我现在如何将OnQueryTextListener设置为SearchView? 之前它正在处理: SearchView.OnQueryTextListener queryTextListener; MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView =(SearchView)searchItem.getActionView(); –

+0

@GregoryWullimann它现在应该像一个活动内部的普通视图,所以只需在活动中使用“findViewById”。 – slanecek

+0

谢谢,我有一个android.widget.SearchView和android.support.v7.widget.SearchView之间的小问题,似乎现在工作。 感谢您的帮助! :) –

1

我在代码中设置了actionBar.setDisplayHomeAsUpEnabled(true)。所以只在工具栏中添加SearchView对我来说不起作用,导航返回和SearchView之间还有一些额外的空间。我在Toolbar没有Title。所以我定制工具栏全是这样的:您的活动

<android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_alignParentTop="true" // If parent layout is RelativeLayout 
     android:background="?attr/colorPrimary" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:contentInsetLeft="0dp" // This is important, 
     app:contentInsetStart="0dp" // Otherwise there will be extra space on Left Side 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center_vertical" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/ivNavBack" 
       android:layout_width="56dp" 
       android:layout_height="56dp" 
       android:background="@drawable/your_drawable" // Custom background (optional), if you want to have a press effect 
       android:padding="16dp" 
       android:src="@drawable/ic_nav_back" /> // You can set icon of your choice here 

      <android.support.v7.widget.SearchView 
       android:id="@+id/searchView" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:queryHint="@string/search_view_hint" 
       app:queryHint="@string/search_view_hint" /> 

     </LinearLayout> 

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

... &:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
SearchView searchView = (SearchView) toolbar.findViewById(R.id.searchView); 
searchView.onActionViewExpanded(); // Expands searchView by default 
// If this is not set, then you will see default search icon and you have to click it to expand SearchView. 

现在没有必要设置actionBar.setDisplayHomeAsUpEnabled(true)代码。只需处理它的点击事件ivNavBack

enter image description here

相关问题