我想通过数据绑定对绑定到布局的实体进行更改后,以编程方式更新工具栏的标题。
摘录自我的布局(最初绑定时,数据绑定工作正常):
androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@{entity.title}"/>
我的活动的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);
}
编辑实体时,我想更新在活动中尝试以下方式的用户界面:
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");