简体   繁体   English

Android:自定义SearchView布局

[英]Android: Customized SearchView Layout

I'm implementing a search interface following android developer guide with the search widget. 我正在按照Android开发人员指南使用搜索小部件实现搜索界面。 The menu is defined in res/menu/options_menu.xml 菜单在res / menu / options_menu.xml中定义

<menu>
    <item android:id="@+id/search"
        android:title="@string/search_view_title"
        android:icon="@drawable/ic_search_white_24dp"
        android:orderInCategory="100"
        app:showAsAction="collapseActionView|ifRoom"
     app:actionViewClass="android.support.v7.widget.SearchView"/>

then inflated in my activity 然后夸大我的活动

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.options_menu, menu);
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    MenuItem searchAction =  menu.findItem(R.id.search);
    SearchView searchView = (SearchView) searchAction.getActionView();

    searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchableActivity.class)));
    searchView.setSubmitButtonEnabled(true);
    searchView.setQueryRefinementEnabled(true);

    return true;
}

It works as expected but what I need now is to add a second TextView under the main one. 它可以按预期工作,但是我现在需要在主窗口下添加第二个TextView。 It will be used for location selection. 它将用于位置选择。 What I need is done in applications like Foursquare or Yelp (screenshots) 我需要在Foursquare或Yelp等应用程序中完成(屏幕截图)

Foursquare 四方

Foursquare搜索小部件

Yelp 喊叫

Yelp搜索小部件

If there is an open source implementation it will be very useful since I will need lot of customizations. 如果有一个开放源代码实现,它将非常有用,因为我将需要大量定制。

I finally found a solution which I think is the less invasive and don't requiere to rewrite a new custom SearchView class. 我终于找到了一种我认为侵入性较小的解决方案,不需要重写新的自定义SearchView类。 SearchView has a main Layout R.layout.abc_search_view containing all its component views . SearchView有一个主布局R.layout.abc_search_view其中包含其所有组件视图。 But they are all aligned horizontally. 但是它们都是水平对齐的。 I don't wand to change the configuration in the existing layout since this has lot of impacts and can change in next versions. 我不希望更改现有布局中的配置,因为这会产生很大的影响,并且可以在下一版本中进行更改。 So I added new LinearLayout as sibling of the existing. 因此,我添加了新的LinearLayout作为现有的同级。 I can then add anything I want in this new Layout. 然后,我可以在此新版式中添加所需的任何内容。 It even possible to move the position of any view from the original (less impact). 甚至可以从原始位置移动任何视图的位置(影响较小)。

MenuItem searchAction =  menu.findItem(R.id.search);
SearchView searchView = (SearchView) searchAction.getActionView();
//Make sure the new layout will be added under the existing SearchView child Layout 
searchView.setOrientation(SearchView.VERTICAL);
LinearLayout locationLayout  = new LinearLayout(this);
locationLayout.setLayoutParams(new LinearLayout.LayoutParams
    (LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT,1f));

TextView searchLocationView = new TextView(this);
searchLocationView.setLayoutParams(new
    SearchView.LayoutParams(SearchView.LayoutParams.MATCH_PARENT,
    SearchView.LayoutParams.WRAP_CONTENT,1f));
searchLocationView.setText("R.string.my_location");
locationLayout.addView(searchLocationView);

Of course you can inflate your new layout from resources. 当然,您可以从资源中扩充新布局。 Note that it's necessary to adapt the height of the toolbar to display the layouts. 请注意,必须调整工具栏的高度以显示布局。

The result in the screenshot below in Layout Inspector (screenshot with LinearLayoutCompat but the result is the same with LinearLayout in the code). 下面的屏幕快照中的布局检查器中的结果(使用LinearLayoutCompat截屏,但代码中的LinearLayout结果相同)。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM