Android 如何更新工具栏标题?

Android 如何更新工具栏标题?,android,android-actionbar,android-databinding,Android,Android Actionbar,Android Databinding,在对通过数据绑定绑定到布局的实体进行更改后,我希望以编程方式更新工具栏的标题 摘自我的布局(最初绑定时数据绑定工作正常): 我的活动的onCreate方法: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidInjection.inject(this); binding

在对通过数据绑定绑定到布局的实体进行更改后,我希望以编程方式更新工具栏的标题

摘自我的布局(最初绑定时数据绑定工作正常):

我的活动的
onCreate
方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidInjection.inject(this);

        binding = DataBindingUtil.setContentView(this, R.layout.my_layout);

        setSupportActionBar(binding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        MyEntity entity = (MyEntity) getIntent().getSerializableExtra(MyEntity.class.getName());
        binding.setEntity(entity);
    }
编辑实体时,我希望更新UI,我在活动中尝试了以下方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   ...
   binding.setEntity(editedEntity); // this seems not to be sufficient and getSupportActionBar().getTitle() still returns the old value
   binding.toolbar.setTitle(editedEntity.getTitle()); // getSupportActionBar().getTitle() returns the updated value but the UI does not show it
   setSupportActionBar(binding.toolbar); // does not help either
   ...
}
因此,问题在于,尽管我可以调试并验证
getSupportActionBar().getTitle()
在某个时刻返回新的和更新的标题,但这种更改不会反映在UI中


如何强制更新工具栏?

您可以使用

getSupportActionBar().setTitle("title");
你可以尝试使用

getSupportActionBar().getTitle() 
setSupportActionBar(binding.toolbar); 
getSupportActionBar().setTitle(editedEntity.getTitle()); 

只是更改了设置工具栏标题和将工具栏设置为supportActionBar的顺序。

否,这不起作用。至少更新后的值没有像我上面提到的那样反映在UI中。您是否使用“android:windowActionBar”和“android:windowNoTitle”属性将活动主题设置为NoActionBar类型?是的,我确实使用了
false
true
。我应该搜索“刷新”。现在我发现了我的问题的原因。我需要将标题绑定到正在折叠的工具栏布局。现在它工作了:)我现在把我的问题标记为重复。不,这不会改变任何事情。我很确定这个问题与数据绑定无关,因为甚至
getSupportActionBar().setTitle(“Updated!”)似乎没有任何结果。
getSupportActionBar().getTitle() 
setSupportActionBar(binding.toolbar); 
getSupportActionBar().setTitle(editedEntity.getTitle());