我有一个BaseActivity,它继承自所有其他子活动,在该BaseActivity中,我设置了工具栏:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(getLayoutId())
if (toolbar != null) {
setSupportActionBar(toolbar)
supportActionBar?.apply {
setDisplayShowTitleEnabled(getActivityTitle() != null)
setDisplayHomeAsUpEnabled(getActivityTitle() != null)
getActivityTitle()?.let {
this.title = getActivityTitle()
}
}
}
}
open fun getActivityTitle(): String? = null
在ChildActitivy上,我有以下代码:
override fun getActivityTitle(): String? {
return getString(R.string.gen_everyone_is_watching)
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<ImageView
android:id="@+id/toolbar_logo"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/ic_logo" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
问题是我想在子活动中显示标题时无法显示标题,而发现标题的唯一方法是不在BaseActivity上设置工具栏。你遇到过类似的事情吗?