[英]Why my toolbar doesn't hide when SearchView action appears?
I am trying to create a search toolbar with Search ActionView, but when I opening the search bar the title of my toolbar is not being hidden.我正在尝试使用 Search ActionView 创建一个搜索工具栏,但是当我打开搜索栏时,工具栏的标题并没有被隐藏。 I tried to add in the menu.xml showAsAction = "collapseActionView" but it remains the same.
我尝试在 menu.xml showAsAction = "collapseActionView" 中添加,但它保持不变。
Here is my code:这是我的代码:
AndroidManifest.xml AndroidManifest.xml
<activity
android:name="com.apps.screens.ContactsActivity"
android:exported="true"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
Activity.java活动.java
public class ContactsActivity extends AppCompatActivity {
private List<Contact> items;
private RecyclerView recyclerView;
private ContactPhoneAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.contacts_menu, menu);
MenuItem item = menu.findItem(R.id.search);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return true;
}
}
menu.xml菜单.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/search"
android:icon="@drawable/ic_search"
android:title="Search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"/>
</menu>
layout.xml布局.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".screens.ContactsActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="Select Contact"
android:background="@color/white" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_contacts" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I appreciate any suggestion我很感激任何建议
In this piece of code, add the following line of code在这段代码中,添加以下代码行
searchview.maxWidth = Integer.MAX_VALUE
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.contacts_menu, menu);
MenuItem item = menu.findItem(R.id.search);
SearchView searchView = (SearchView) item.getActionView();
searchview.maxWidth = Integer.MAX_VALUE // add this line of code
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.