[英]toolbar title not visible after closing searchview widget
In my application there is a activity for searching event.在我的应用程序中有一个用于搜索事件的活动。 In the layout i have added a searchview widget (android.support.v7.widget.SearchView) inside toolbar.
在布局中,我在工具栏中添加了一个搜索视图小部件(android.support.v7.widget.SearchView)。 When the activity is launched the searchview is expanded by default as i have set searchView.setIconified(false).
当活动启动时,搜索视图默认展开,因为我已经设置了 searchView.setIconified(false)。
Searchview when expanded.展开时的搜索视图。
The problem is if i close the searchview by taping on the "X" close button, the toolbar title is not visible and the search icon is shown right after the arrow back button.
问题是,如果我通过点击“X”关闭按钮关闭搜索视图,则工具栏标题不可见,并且搜索图标显示在箭头后退按钮之后。
Searchview after taping on the "X" close button.点按“X”关闭按钮后的 Searchview。
EventSearchResultActivity.java file EventSearchResultActivity.java 文件
package xyz.bnayagrawal.android.icsapp.event;
import android.app.SearchManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import xyz.bnayagrawal.android.icsapp.R;
public class EventSearchResultActivity extends AppCompatActivity {
private String query_string;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_search_result);
Toolbar toolbar = (Toolbar) findViewById(R.id.search_event_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
SearchView searchView = (SearchView) findViewById(R.id.search_event_searchView);
searchView.setIconified(false);
searchView.clearFocus();
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query_string = intent.getStringExtra(SearchManager.QUERY);
searchView.setQuery(query_string, false);
//Perform search
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_event_search_result.xml layout file activity_event_search_result.xml 布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
android:background="?attr/layoutBackground"
tools:context="xyz.bnayagrawal.android.icsapp.event.EventSearchResultActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:background="@android:color/white">
<android.support.v7.widget.Toolbar
android:id="@+id/search_event_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="3dp"
app:title="Search Event">
<android.support.v7.widget.SearchView
android:id="@+id/search_event_searchView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="-16dp"
android:paddingStart="-16dp"
android:queryHint="Search event..."
app:queryHint="Search event...">
</android.support.v7.widget.SearchView>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
I have set the width of the searchview to match_parent, that's why its hiding the toolbar content.我已将搜索视图的宽度设置为 match_parent,这就是它隐藏工具栏内容的原因。 If it were a menu item i could have added [app:showAsAction="collapseActionView|ifRoom"] this line.
如果它是一个菜单项,我可以添加 [app:showAsAction="collapseActionView|ifRoom"] 这一行。 I want the search icon to be floated left (like if it were a menu item) and the toolbar title visible.
我希望搜索图标向左浮动(就像它是一个菜单项)并且工具栏标题可见。 How to solve this?
如何解决这个问题? Sorry for my bad code formatting.
抱歉我的代码格式不好。
在全局声明“工具栏”变量并使用此代码关闭 SearchView
toolbar.collapseActionView()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.