我有以下布局:
<LinearLayout 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:orientation="vertical"
tools:context=".activities.MainActivity">
<include
android:id="@+id/app_toolbar"
layout="@layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout" />
菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:icon="@drawable/ic_settings_white_24dp"
app:showAsAction="never" />
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_white_24dp"
android:title="@string/search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
和searchView的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
this.searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
//searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
search(query);
return true;
}
});
return true;
}
tablayout包含2个用于不同目的的标签。 我的工具栏中还有一个searchview作为选项菜单。 我希望searchview在单击searchview时隐藏工具栏以及tablayout。您可以查看Whatsapp或Youtube搜索功能,了解我的意思。
有什么想法吗? 感谢
答案 0 :(得分:0)
为此,您可以像这样创建自定义搜索视图。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_alignParentTop="true"
android:orientation="vertical">
<LinearLayout
android:id="@+id/searchViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shadow"
android:orientation="horizontal"
android:visibility="gone">
<ImageView
android:id="@+id/ic_back"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|start"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:contentDescription="@string/app_name"
android:gravity="center|start"
android:padding="5dp"
android:src="@drawable/backspase" />
<EditText
android:id="@+id/search_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:focusable="true"
android:gravity="center|start"
android:hint="@string/search_hint"
android:imeOptions="actionDone"
android:padding="5dp"
android:singleLine="true" />
<ImageView
android:id="@+id/clearSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:contentDescription="@string/app_name"
android:padding="5dp"
android:src="@drawable/ic_cross" />
</LinearLayout>
<include layout="@layout/toolbar" />
</LinearLayout>
<View
android:id="@+id/shadow"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="@+id/topLayout"
android:background="@color/colorPrimary"
android:visibility="visible" />
<android.support.design.widget.TabLayout
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/shadow"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/ColorWhite"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/ColorWhite"
app:tabTextColor="#85A3B1" />
<peacocktech.in.mybulletin.utils.NonSwipeableViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabHost" />
</RelativeLayout>
之后使用此代码进行活动
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 1, Menu.NONE, getResources().getString(R.string.action_update)).setIcon(R.drawable.ic_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case 1:
// Visible Search layout (R.id.searchViewLayout)
preferencesUtility.setString("search_open", "true");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
onbackpress检查searchview是否打开
@Override
public void onBackPressed() {
if (preferencesUtility.getString("search_open").equals("true")) {
// Hide/Visibility gone Search layout (R.id.searchViewLayout)
} else {
finish
}
}
不显示/隐藏工具栏 只设置searchViewLayout可见性隐藏/显示。