gpt4 book ai didi

android - 在 onMenuItemActionExpand 上显示 SearchView 内的按钮

转载 作者:行者123 更新时间:2023-11-29 02:33:12 25 4
gpt4 key购买 nike

我在工具栏上有 SearchView,我想在展开时在 SearchView 中添加时间按钮,在折叠时隐藏。

enter image description here

onCreateOptionsMenu

@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.nearby_friends_menu, menu);

MenuItem item = menu.findItem(R.id.search_PTL);

searchView = new SearchView(getActivity());
searchView.setIconified(true);

MenuItemCompat.setActionView(item, searchView);
Button timeButton = new Button(getActivity());
LinearLayout.LayoutParams navButtonsParams = new LinearLayout.LayoutParams(toolbar.getHeight() * 2 / 3, toolbar.getHeight() * 2 / 3);
// LinearLayout.LayoutParams navButtonsParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
timeButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_access_time_black_24dp));
((LinearLayout) searchView.getChildAt(0)).addView(timeButton, navButtonsParams);
((LinearLayout) searchView.getChildAt(0)).setGravity(Gravity.CENTER_VERTICAL);

searchView.setQueryHint("Search User & Business");

MenuItem.OnActionExpandListener expandListener = new MenuItem.OnActionExpandListener() {@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
Log.d(TAG, "On Menu Expand");
//THIS METHOD NEVER CALLED
//timeButton.setVisibility(View.GONE);
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
Log.d(TAG, "On Menu Collapse");
//THIS METHOD NEVER CALLED
return true;
}
};
item.setOnActionExpandListener(expandListener);

菜单.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/notificationBell"
android:title=""
android:orderInCategory="200"
android:icon="@drawable/notification"
app:actionLayout="@layout/menuitem_notification_count"
android:visible="true"
app:showAsAction="always">
</item>
<item android:id="@+id/search_PTL"
android:title="Search"
android:orderInCategory="100"
android:icon="@drawable/places_ic_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>

</menu>

这就是我从上面的代码中得到的 enter image description here

我需要在 searchview 中放置时间按钮,它在 searchview 展开时可见,在折叠时隐藏。

最佳答案

您可以使用 SpannableStringBuilder

带注释的代码 fragment :

    AutoCompleteTextView text = searchView.findViewById(R.id.search_src_text);// get reference of textview of SearchView
if (text != null) {

SpannableStringBuilder magHint = new SpannableStringBuilder(" ");
magHint.append("Search ...");//set text
myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());//myDrawable image you want to set
ImageSpan span = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE);
magHint.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//sets image span
text.setHint(magHint);//set string for searchView hint
}

关于android - 在 onMenuItemActionExpand 上显示 SearchView 内的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48498756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com