Android Menu Item Title not displaying

大兔子大兔子 提交于 2020-01-02 03:36:28

问题


I've been fiddling with android, and I've been following the menu sample, and it runs almost fine, but the "title" field of the menu items isn't displaying.

I think it's related to this question: https://stackoverflow.com/questions/3286093/android-menu-item-not-showing-text but I'm not sure what his answer means.

Anyway, it properly gets that I have 2 menu items, it just isn't displaying the text. I'm not quite sure where the error is and figured extra sets of eyes would be good.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="https://schemas.android.com/apk/res/android">
 <item android:id="@+id/options"
  android:title="@string/main_options" />
 <item android:id="@+id/options2"
  android:title="@string/main_options2" />
</menu>

Inflator:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

If anything else is needed, let me know.

edit: string file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World!</string>
    <string name="app_name">LifeInColor</string>
 <string name="main_options">Options</string>
 <string name="main_options2">Something goes here</string>
</resources>

changing the xml to:

 <?xml version="1.0" encoding="UTF-8"?>
    <menu xmlns:android="https://schemas.android.com/apk/res/android">
     <item android:id="@+id/options"
      android:title="@string/main_options" />
     <item android:id="@+id/options2"
      android:title="Something goes here" />
    </menu>

gets the same result. I have a picture, but it won't let me post it because I'm new.


回答1:


In your menu file replace https://schemas.android.com/apk/res/android with http://schemas.android.com/apk/res/android (https -> http). So it looks like the error was due to incorrect schema address.




回答2:


Maybe the icon is too large. If you use an icon of more than 48x48 on an hdpi screen, the user will either see no title or see a truncated title.

See http://developer.android.com/guide/practices/ui_guidelines/icon_design_menu.html#size9




回答3:


I had to specify an icon with android:icon="@null"to each item, then the title was shown. This works if using Toolbar.




回答4:


Directly we can add menu items like this , it working fine for me

       @Override
        public boolean onCreateOptionsMenu(Menu menu) 
           {
        // TODO Auto-generated method stub
             super.onCreateOptionsMenu(menu);
             MenuItem item1=menu.add(0, 4, 0,"text1");
            item1.setIcon(R.drawable.car);
            MenuItem item2=menu.add(0, 0, 0, "text2");
             item2.setIcon(R.drawable.share);
            MenuItem item3=menu.add(0, 2, 0, "text3");
           item3.setIcon(R.drawable.history);
           MenuItem item4=menu.add(0, 3, 0, "text4");
           item4.setIcon(R.drawable.settings);
    return true;
}


来源:https://stackoverflow.com/questions/3764061/android-menu-item-title-not-displaying

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!