Я делаю приложение с переключением тем. Но после установки новой темы, когда я вызываю setContentView (), панель действий не отображается. Пример изображения
MainActivity.kt:
override fun setContentView(layoutResID: Int) {
super.setContentView(layoutResID)
val toolbar = findViewById(R.id.toolbar) as? Toolbar
if(toolbar!=null){ setSupportActionBar(toolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)}
}
fun setMaterialTheme(newTheme: Int) {
setTheme(newTheme)
setContentView(R.layout.settings_fragment)
saveTheme(newTheme)
}
main-activity. xml:
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:foregroundTint="#CDFFFFFF"
android:outlineProvider="paddedBounds"
app:layout_constraintBottom_toTopOf="@+id/textInputLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:subtitleTextColor="#CDFFFFFF"
app:titleTextColor="#CDFFFFFF" />
Я также звоню из фрагмент:
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
nightThemeSwitch = view?.findViewById(R.id.night_theme_switch)
super.onViewCreated(view, savedInstanceState)
readSettings()
nightThemeSwitch?.setOnCheckedChangeListener{
buttonView: CompoundButton, isChecked: Boolean ->
if (isChecked) {
(activity as MainActivity).setMaterialTheme(R.style.AppTheme_Night)
} else {
(activity as MainActivity).setMaterialTheme(R.style.AppTheme)
}
}
}