我已经尝试了数小时来在底部(次要)android.support.v7.widget.ToolBar中设置一个简单的android.support.v7.widget.SearchView。我已经在顶部使用另一个工具栏作为ActionBar,因此我无法在活动的
相关代码,简化后的HomepageActivity:
简化的主页布局
如果我像那样运行它,我会得到一个SearchView,但没有我的
onCreateOptionsMenu
回调上使用它,而是必须设置一个OnMenuItemClickListener()
。我已经阅读了Android SearchView教程,以及关于工具栏的这个教程。相关代码,简化后的HomepageActivity:
package something.somethingelse.activities;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnCloseListener;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
public class HomepageActivity extends AppCompatActivity {
protected static final String TAG = "HomepageActivity";
private Toolbar mTopToolbar;
private Toolbar mBottomToolbar;
private FloatingActionButton mFab;
private Context mContext;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTopToolbar = (Toolbar) findViewById(R.id.topToolbar);
setSupportActionBar(mTopToolbar);
mBottomToolbar = (Toolbar) findViewById(R.id.bottomToolbar);
mBottomToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
SearchManager searchManager = (SearchManager) HomepageActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (menuItem != null) {
Log.d(TAG, "menuItem != null");
searchView = (SearchView) menuItem.getActionView();
}
if (searchView != null) {
Log.d(TAG, "searchView != null");
searchView.setSearchableInfo(searchManager.getSearchableInfo(HomepageActivity.this.getComponentName()));
}
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
mBottomToolbar.setVisibility(View.GONE);
return false;
}
});
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
Log.d(TAG, "Submited: "+arg0);
return false;
}
@Override
public boolean onQueryTextChange(String arg0) {
Log.d(TAG, "Changed: "+arg0);
return false;
}
});
return false;
}
});
mBottomToolbar.inflateMenu(R.menu.bottom_toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
简化的主页布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:guide="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="@+id/topToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/white"
guide:elevation="4dp"
android:minHeight="?attr/actionBarSize"
guide:theme="@style/Toolbar_Theme" >
<ImageView
android:id="@+id/ivAb_logo"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="58dp"
android:paddingTop="10dp"
android:src="@drawable/generic_logo" />
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/topToolbar" >
<RelativeLayout
android:id="@+id/homepage_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/homepage_background"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/bottomToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/primaryDark"
guide:elevation="4dp" />
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="220dp"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:background="#60BBBBBB"
android:choiceMode="singleChoice"
android:divider="#FFFFFF"
android:dividerHeight="1px" />
</android.support.v4.widget.DrawerLayout>
菜单 bottom_toolbar.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:guide="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:title="@string/search"
android:icon="@drawable/ic_search_white"
guide:actionViewClass="android.support.v7.widget.SearchView"
guide:showAsAction="always">
</item>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search"
android:label="@string/app_name"
/>
与 AndroidManifest.xml 相关的部分
<activity
android:name="something.somethingelse.activities.HomepageActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/Theme.Default" >
<intent-filter>
<action android:name="something.somethingelse.activities.HomepageActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
如果我像那样运行它,我会得到一个SearchView,但没有我的
android:icon="@drawable/ic_search_white"
,相反,我得到了Google SearchView图标,如果我点击它,我会得到一个漂亮的编辑文本,带有清除和搜索功能,但无论我输入或按什么键,Toolbar.OnMenuItemClickListener
内部的任何Log.d都不会被触发。(由于声望问题,我无法发布图片。)如果我使用android:actionViewClass="android.support.v7.widget.SearchView"
而不是guide:actionViewClass="android.support.v7.widget.SearchView"
,我会得到我的android:icon="@drawable/ic_search_white"
,如果我点击它,我会在searchView.setOnCloseListener(new OnCloseListener() {
上得到一个NPE,因为searchView
为空。我尝试了this相关问题中的方法,以及this,但迄今为止没有成功。
onMenuItemClick()
。您说它不能接收点击事件是什么意思?是指当您单击键盘上的放大镜图标时没有任何反应吗?对我来说,这会触发onNewIntent()
回调函数。 - Nikiforos