答案 0 :(得分:1)
这是我用过的解决方案
为操作栏创建自定义布局并以此方式设置
ActionBar actionBar = getSupportActionBar();
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
View view = getSupportActionBar().getCustomView();
// find ids using view.
imageButton = (ImageButton)view.findViewById(R.id.action_bar_back);
在该自定义布局中添加所有视图,这些视图将在单击搜索之前和单击搜索之后显示 单击搜索图标后,每个视图都应该可见,默认情况下,可以在XML中看到GONE,
返回,加州,默认情况下可见搜索图标
当用户点击搜索图标后,加利福尼亚,搜索图标可见性GONE并使X图标和搜索酒店可见
答案 1 :(得分:1)
这可以帮助您并满足您的要求。虽然它不是完美的解决方案,但它对我来说是设置工具栏上方的视图。
使用弹出窗口并扩大搜索布局。
设计popupwindow位置,animationstyle,背景等..并参考此Using Immersive Full-Screen Mode以获取systemUiVisibility用法。
PopupWindow mpopup; //declare it globally.
//it will make main background transperent. where mRoot is the root layout.
mRoot.setAlpha(0.50f);
View popUpView = getLayoutInflater().inflate(R.layout.activity_search,
null);
popUpView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
// Creation of popup
mpopup = new PopupWindow(popUpView, RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT, true);
//this will set the transperency of background.and set the background color transperent.
mpopup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//set the animation style of popup view.
mpopup.setAnimationStyle(android.R.style.Animation_Dialog);
mpopup.showAtLocation(popUpView, Gravity.NO_GRAVITY, 0, 0);
FloatingActionButton btnSearch = (FloatingActionButton) popUpView.findViewById(R.id.btnsearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mpopup.dismiss();
//remove transperency
mRoot.setAlpha(1f);
}
});
activity_search.xml :(根据您的要求设计您的布局只是示例)
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coorlay"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/lay_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/textSecondary"
android:orientation="vertical"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Write something" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="10dp">
<CheckBox
android:id="@+id/check1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="check1" />
<CheckBox
android:id="@+id/check2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="check2" />
<CheckBox
android:id="@+id/check3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="check3" />
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/btnsearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/places_ic_search"
android:tint="@color/white"
app:layout_anchor="@id/lay_search"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
现在,当您单击工具栏的搜索按钮时,将显示上面的视图。并且对于根布局的背景转换,您可以在buttonclick上设置其alpha并在按钮关闭时删除alpha。如代码中所示。
答案 2 :(得分:0)
您可以使用以下代码添加搜索选项,并根据您的需要对其进行自定义。
@Override
public boolean onCreateOptionsMenu(final android.view.Menu menu) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = new SearchView(this);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getString(R.string.hint_search));
searchView.setIconifiedByDefault(false);
ImageView searchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
searchIcon.setImageResource(R.drawable.ic_search);
SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(Color.parseColor("#888888"));
searchAutoComplete.setTextColor(ContextCompat.getColor(context, R.color.text));
ImageView searchCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchCloseIcon.setImageResource(R.drawable.ic_clear);
ImageView voiceIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn);
voiceIcon.setImageResource(R.drawable.ic_keyboard_voice);
voiceIcon.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
menu.getItem(0).collapseActionView();
return false;
}
});
menu.add(getString(R.string.title_search))
.setIcon(R.drawable.ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
MenuItemCompat.setOnActionExpandListener(menu.getItem(0), new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
AppGlobal.hideSoftKeyboard(context);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
searchView.requestFocus();
AppGlobal.openSoftKeyboard(context);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
return true;
}
});
return true;
}
答案 3 :(得分:0)
您可以使用弹出窗口或在工具栏中创建布局,如下所示:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimaryDark"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="40dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="California"/>
<TextView
android:id="@+id/subtitle"
android:layout_below="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="California"/>
<Button
android:id="@+id/single_button"
android:layout_below="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="California"/>
<LinearLayout
android:id="@+id/ll_button"
android:layout_below="@+id/single_button"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="California"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="California"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:id="@+id/tv_spinner"
android:layout_below="@+id/ll_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="spinner"/>
</LinearLayout>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp"
android:layout_marginTop="173dp" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
它看起来像这样:
您可以根据自己的要求显示/隐藏视图。
答案 4 :(得分:0)
@Dipak请点击链接PersistentSearch这比您想要的更多, 我在我的代码中使用它非常棒。