我正在尝试理解如何在Android中在我的工具栏中实现SearchView。问题在于,根据我选择的配置,我会得到不同的界面结果。
这是我的Activity的布局:
这是我的toolbar_shadow布局:
这是我的搜索项的布局文件:
使用此配置,我得到了以下输出: 但是如果我点击放大镜,就会出现这个: 如果我开始打字,我会得到这个: 所以我认为是因为我有这个配置
“这是我活动中初始化搜索视图的部分:”
这是我初始化工具栏的方法:
这是我的Activity的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:context="com.construct.v2.activities.teams.ActivityTeams"
android:background="#ffffff"
android:id="@+id/relativeLayout">
<include android:id="@+id/toolbar" layout="@layout/toolbar_shadow"/>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/toolbar" />
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorColor="#125688"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
这是我的toolbar_shadow布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/PopupMenuStyle"
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:titleTextColor="@android:color/black"
android:layout_alignParentTop="true"
android:gravity="right"
android:elevation="5dp">
<TextView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/s_text_size"
android:padding="@dimen/small_margin"
android:layout_gravity="right|end"
android:layout_marginEnd="@dimen/s_margin"
android:textColor="#000000"
android:text="@string/edit"/>
</android.support.v7.widget.Toolbar>
这是我的搜索项的布局文件:
<?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:title="@string/search"
app:icon="@drawable/ic_search_black_24dp"
android:icon="@drawable/ic_search_black_24dp"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
使用此配置,我得到了以下输出: 但是如果我点击放大镜,就会出现这个: 如果我开始打字,我会得到这个: 所以我认为是因为我有这个配置
app:showAsAction="collapseActionView|ifRoom"
,但我尝试更改为 always
,但我得到了这个输出:
放大镜消失了。但如果我点击它应该在的位置,我会得到这个输出:
“这是我想要的正确输出,但我不知道为什么放大镜没有显示出来。”“这是我活动中初始化搜索视图的部分:”
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search_team, menu);
SearchManager searchManager = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) searchMenuItem.getActionView();
ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchClose.setImageResource(R.drawable.ic_action_cancel_black);
for (TextView textView : findChildrenByClass(searchView, TextView.class)) {
textView.setTextColor(Color.BLACK);
textView.setHintTextColor(Color.BLACK);
}
searchView.setSearchableInfo(searchManager.
getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return true;
}
这是我初始化工具栏的方法:
@Override
protected void initToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar!=null) {
if (user_type == 1) {
toolbar.setTitle(R.string.collaborators_title);
} else if (user_type == 2) {
toolbar.setTitle(R.string.admin_title);
}
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_action_back_black);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//toolbar.setTitle("Teste");
//toolbar.setTitleTextColor(Color.parseColor("#000000"));
//final TextView edit = (TextView) findViewById(R.id.delete);
//edit.setText(" ");
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
}