我试图从一组片段中添加一个项目到选项菜单。
我已经创建了一个新的MenuFragment类,并扩展了我希望包含菜单项的片段。代码如下:
Java:
public class MenuFragment extends Fragment {
MenuItem fav;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
fav = menu.add("add");
fav.setIcon(R.drawable.btn_star_big_off);
}
}
科特林:
class MenuFragment : Fragment {
lateinit var fav: MenuItem
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
fav = menu.add("add");
fav.setIcon(R.drawable.btn_star_big_off);
}
}
由于某种原因,onCreateOptionsMenu似乎没有运行。
在膨胀菜单之前,您需要使用menu.clear()。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
and
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
如果上面的选项都不适合你,在你的片段中试试这个:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
....
toolBar = rootView.findViewById(R.id.import_contacts_toolbar)
toolBar?.title = "Your title"
toolBar?.subtitle = "yor subtitile"
contactsActivity().setSupportActionBar(toolBar)
toolBar?.inflateMenu(R.menu.import_contacts_menu)
...
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.1 -> {
return true
}
R.id.2 -> {
return true
}
}
return false
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
menu.clear()
inflater.inflate(R.menu.import_contacts_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
val search = menu.findItem(R.id.action_search)
val searchView = search.actionView as SearchView
searchView.requestFocus()
val txtSearch = searchView.findViewById<View>(androidx.appcompat.R.id.search_src_text) as EditText
txtSearch.hint = "Search..."
txtSearch.setHintTextColor(Color.WHITE);
txtSearch.setTextColor(Color.WHITE)
try {
val f: Field = TextView::class.java.getDeclaredField("mCursorDrawableRes")
f.setAccessible(true)
f.set(txtSearch, R.drawable.search_edit_text_cursor)
} catch (ignored: Exception) {
Log.d(TAG, "failed to expose cursor drawable $ignored")
}
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
return false
}
override fun onQueryTextChange(newText: String): Boolean {
return true
}
})
searchView.setOnCloseListener {
}
}
在我的例子中,我有一个搜索菜单项,它被设置为始终可见。这是它的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/action_search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:title="Search"/>
<item android:id="@+id/1"
android:title="1">
</item>
<item android:id="@+id/2"
android:title="2">
</item>
</menu>