简体   繁体   English

Android-编辑动作栏按钮文字

[英]Android - Edit Action Bar Button text

I cannot find any way to edit the text of a button that shows on the action bar. 我找不到任何方法来编辑显示在操作栏上的按钮的文本。

res/menu.main.xml
<item
    android:id="@+id/action_data"
    android:title="@string/action_date"
    app:showAsAction="withText|ifRoom"/>

And this what and how i'm trying to achive it: 这就是我试图达到的目标和方式:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MenuItem b = (MenuItem) findViewById(R.id.action_data);
    b.setTitle(new SimpleDateFormat("dd-MM-yy").format(c));
}

This is the Button i need to change the title of: 这是我需要更改的标题的按钮: 在此处输入图片说明 When i do all this it just crashes! 当我完成所有这些操作时,它只会崩溃! Thanks for your help! 谢谢你的帮助!

您是否尝试过setTitleCondensed,我刚刚检查了文档,似乎标题太长且空间不足,首选方法是使用setTitleCondensed。

call ActivityCompat.invalidateOptionsMenu(this.getActivity) to reload the onCreateOptionMenu for android version greater than 11 调用ActivityCompat.invalidateOptionsMenu(this.getActivity)以重新加载大于11的android版本的onCreateOptionMenu

MenuItem b = (MenuItem) findViewById(R.id.action_data);
b.setTitle(new SimpleDateFormat("dd-MM-yy").format(c));
ActivityCompat.invalidateOptionsMenu(this.getActivity)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM