如何创建在res \菜单一个TextView?菜单、res、TextView

2023-09-06 11:28:48 作者:七弦

我想在菜单中创建一个TextView,将文本动态设置。现在我的菜单上有这样的:

I'm trying to create a TextView in the menu, the text is to be set dynamically. Right now my menu has this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="ifRoom"
        android:title="Search"/>
    <item
        android:id="@+id/action_sync"
        android:icon="@drawable/ic_menu_refresh"
        android:showAsAction="ifRoom"
        android:title="Sync"/>

</menu>

我如何创建它只有文字的项目?

How can I create an item which has only text?

推荐答案

如果你想只显示文字,根本就不在menu.xml文件添加一个图标。如果你想改变code中的文字,你可以在你的活动回调象这样在做这样的prepareOptionsMenu():

If you want to just show text, simply don't add an icon in menu.xml. If you want to change the text in code, you can do so in the onPrepareOptionsMenu() on your activity callback as such:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem myItem = menu.findItem(R.id.the_item);
    myItem.setTitle("bla");
    return true;
}