简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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