我在我的活动中有这个代码:
protected void onCreate(Bundle savedInstanceState) {
...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
...
}
Run Code Online (Sandbox Code Playgroud)
我正在从以下各种片段更新ActionBar标题onResume():
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(title);
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但在更改方向后,标题再次更改为应用程序名称.我怎么能克服这个?
编辑:经过调查更多,我试过这个,并发现这个奇怪的行为:添加此代码,我在片段中设置标题:
final ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
if (actionBar != null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d("SWAPNIL", "IN RUN BEFORE: " + actionBar.getTitle());
actionBar.setTitle(title);
Log.d("SWAPNIL", "IN RUN AFTER : " + actionBar.getTitle());
}
}, …Run Code Online (Sandbox Code Playgroud) 我想在对通过数据绑定绑定到布局的实体进行更改后以编程方式更新工具栏的标题。
我的布局摘录(数据绑定在最初绑定时工作正常):
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}"/>
Run Code Online (Sandbox Code Playgroud)
我的活动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);
}
Run Code Online (Sandbox Code Playgroud)
编辑实体时,我想更新 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 …Run Code Online (Sandbox Code Playgroud) android ×2