2015-03-31 49 views
1

我学会了如何从http:/developer.android.com/training/search/setup.html上的操作栏(我使用工具栏)添加搜索视图。我可以添加该功能,但是当我想添加语音搜索时,不显示麦克风图标。它看起来像我只需要添加android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"为什么我的工具栏搜索操作视图中没有显示语音搜索?

我还有一些其他问题。

  1. 我设置了searchView.setIconifiedByDefault(false);但是为什么它仍默认图标?
  2. 当我关注其他小部件时,如何使内容视图回到图标?

http://i.stack.imgur.com/FUk3T.png

http://i.stack.imgur.com/6cFnV.png

这里是我的代码:

MainActivity.java

public class MainActivity extends ActionBarActivity { 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 

 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
 
     setSupportActionBar(toolbar); 
 

 
     handleIntent(getIntent()); 
 
    } 
 

 
    private void handleIntent(Intent intent) { 
 
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
 
      String query = intent.getStringExtra(SearchManager.QUERY); 
 
      TextView textView = (TextView) findViewById(R.id.query); 
 
      textView.setText(query); 
 
     } 
 
    } 
 

 
    @Override 
 
    protected void onNewIntent(Intent intent) { 
 
     setIntent(intent); 
 
     handleIntent(intent); 
 
    } 
 

 
    @Override 
 
    public boolean onCreateOptionsMenu(Menu menu) { 
 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
 

 
     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
 
     SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); 
 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
 
     searchView.setIconifiedByDefault(false); 
 
     return true; 
 
    } 
 
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
 
    android:paddingRight="@dimen/activity_horizontal_margin" 
 
    android:paddingTop="@dimen/activity_vertical_margin" 
 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 
 

 
    <android.support.v7.widget.Toolbar 
 
     xmlns:android="http://schemas.android.com/apk/res/android" 
 
     android:id="@+id/toolbar" 
 
     android:layout_height="wrap_content" 
 
     android:layout_width="match_parent" 
 
     android:minHeight="?attr/actionBarSize" /> 
 

 
    <TextView 
 
     android:id="@+id/query" 
 
     android:text="@string/hello_world" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_below="@id/toolbar"/> 
 

 
</RelativeLayout>

searchable.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:hint="@string/search_hint" 
 
    android:label="@string/app_name" 
 
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>

回答

0

试试这个..

public boolean onCreateOptionsMenu(Menu menu) { 
     this.menu = menu; 
     MenuInflater menuInflater = getMenuInflater(); 
     menuInflater.inflate(R.menu.menu_main, menu); 

     MenuItem searchItem = menu.findItem(R.id.action_search); 
     searchView = (SearchView) searchItem.getActionView(); 
     searchView.setQueryHint(getResources().getString(R.string.hint)); 

     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 

} 
+0

它没有任何区别 – stackex 2015-03-31 06:12:32

相关问题