2016-04-28 76 views
13

enter image description here如何将SearchView的搜索图标移动到右侧?

这是我的布局,使用android.support.v7.widget.SearchView。 我想搜索图标移动到右侧,但我还没有发现任何方法... `

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways|snap" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

     <android.support.v7.widget.SearchView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </android.support.v7.widget.Toolbar> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_main_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</android.support.design.widget.AppBarLayout> 

`

后,我单击该图标,它会改变这一布局 enter image description here

回答

15

你可以做到这一点编程

SearchView search = (SearchView) item.getActionView(); 
    search.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT)); 
+0

thx!它完美的作品,它不会涵盖我的标题! – meunicorn

7
<android.support.design.widget.AppBarLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:theme="@style/AppTheme.AppBarOverlay"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:layout_scrollFlags="scroll|enterAlways|snap" 
    app:popupTheme="@style/AppTheme.PopupOverlay"> 

    <RelativeLayout 
     android:id="@+id/not" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
         > 
    <android.support.v7.widget.SearchView 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 
</android.support.v7.widget.Toolbar> 

<android.support.design.widget.TabLayout 
    android:id="@+id/tab_main_title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
</android.support.design.widget.AppBarLayout> 
+0

谢谢!它工作得很好! – meunicorn

1

尝试......看起来,你expect.Just添加搜索查看标签内这条线。

安卓重力= “右”

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:layout_scrollFlags="scroll|enterAlways|snap" 
    app:popupTheme="@style/AppTheme.PopupOverlay"> 

    <android.support.v7.widget.SearchView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="right" /> 
</android.support.v7.widget.Toolbar> 

<android.support.design.widget.TabLayout 
    android:id="@+id/tab_main_title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

+3

感谢您的回答,但它不起作用 – meunicorn

+0

发布您的完整代码。因为我已经测试过你的片段。它很有用。 –

9

您可以像menu.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"> 
    <item 
     android:id="@+id/action_search" 
     android:icon="@mipmap/ic_search_white_24dp" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:showAsAction="ifRoom|collapseActionView" /> 
    <item android:id="@+id/action_download" 
     android:title="@string/menu_action_download" 
     android:icon="@mipmap/ic_file_download_white_24dp" 
     android:orderInCategory="100" 
     app:showAsAction="always" /> 
</menu> 

Normal look Collapsed look

0

在你想在任何其他视图中使用SearchView以外的ActionBar情况下(让我们说,例如ConstraintLayout),可以使用贴在this网站的方法。基本上该图标位于布局开始处的ImageView(或者我可以说“它是添加到其父项”“的第一个元素)。使用此代码,您正在搜索它,然后将其从父文件中删除,最后将其还原。其结果是:它将被定位在解决了SearchView部件......问题的“结束” ...

//Get ImageView of icon 
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon); 

//Get parent of gathered icon 
ViewGroup linearLayoutSearchView = (ViewGroup) searchViewIcon.getParent(); 
//Remove it from the left... 
linearLayoutSearchView.removeView(searchViewIcon); 
//then put it back (to the right by default) 
linearLayoutSearchView.addView(searchViewIcon); 

测试工作,满足...;)

0

SearchView重力设置为通过XML的权利或以编程方式工作,但当SearchView展开时,它是在右边的一个小区域的宽度,这不是我想要的预期外观,我希望它采取全宽。

注1:该解决方案不允许标题显示,因此我添加了TextView来保存标题。

注2:当SearchView扩大,它会在自定义标题重叠,所以我做了SearchView背景的工具栏,以避免继续隐藏/显示标题与SearchView的展开/折叠

这里是Toolbar代码:

​​

活动里面,我所用的关闭和SearchClick听众对准SearchView左侧。

mSearchView = findViewById(R.id.search_view); 

    mSearchView.setOnCloseListener(new SearchView.OnCloseListener() { 
     @Override 
     public boolean onClose() { 
      RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) mSearchView.getLayoutParams(); 
      param.removeRule(RelativeLayout.ALIGN_PARENT_LEFT); 
      //set layout params to cause layout update 
      mSearchView.setLayoutParams(param); 
      return false; 
     } 
    }); 
    mSearchView.setOnSearchClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) mSearchView.getLayoutParams(); 
      param.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
      //set layout params to cause layout update 
      mSearchView.setLayoutParams(param); 
     } 
    });