2016-05-29 127 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

相關問題