[英]Android: Add a custom button to SearchView Widget in Actionbar
[英]Android SearchView with custom ActionBar
我想使用自定义操作栏创建搜索视图,就像在“操作栏”中创建搜索视图一样。 我不知道该怎么做。我在网上冲浪,但大多数结果将显示带有操作栏而不是自定义操作栏的搜索视图。但是我想在自定义操作栏上添加搜索视图。帮我 。
这是我自定义的操作条形码
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.all_post);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
dbhelper = new MyDbHelper(this);
dbhelper.onOpen(db);
Intent intent = new Intent(CustomActionActivity.this, MyService.class);
startService(intent);
ActionBar mActionBar = getSupportActionBar();
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
ImageView imgSearchBtn = (ImageView)mCustomView.findViewById(R.id.imgSearchButton);
imgSearchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click on ", " Searchview !!!! ");
}
});
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);
mActionBar.setCustomView(mCustomView);
Toolbar parent =(Toolbar) mCustomView.getParent();
parent.setContentInsetsAbsolute(0, 0);
}
我想通过折叠和展开功能在按钮单击时调用搜索视图。
ImageView imgSearchBtn = (ImageView)mCustomView.findViewById(R.id.imgSearchButton);
imgSearchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click on ", " Searchview !!!! ");
}
});
谢谢 。
我看到您将Toolbar
用作自定义的ActionBar
(请注意,您必须在styles.xml中使用NoActionBar样式,然后将Toolbar
手动添加到布局中)您仍然可以使用菜单充气器:
search_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>
只需设置setSupportActionBar(mToolbar);
mToolbar是您的Toolbar
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
if (searchView != null) {
// do something with it
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.