【发布时间】:2019-09-06 09:10:52
【问题描述】:
所以,我尝试在 1 中创建底部导航栏和导航抽屉 活动(我使用了新的活动抽屉模板并将其与 我从底部导航活动中获得的底部导航视图标签)。一世 想要更改工具栏和底部导航的主题 运行时抽屉(所以我考虑在活动中使用 setTheme) 但 setTheme 不起作用(颜色不会改变) 我尝试在 XML 上的每个属性上设置主题(在 AppBarLayout 和 BottomNavigationView 容器标签),它确实有效。但是当我尝试 通过以编程方式,我在他们的上下文中设置了活动,但确实如此 什么都没有
contain_main_container.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
android:theme="@style/AppNightTheme"
tools:context=".MainContainerActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
app:itemBackground="?colorPrimary"
app:itemIconTint="@drawable/nav_item_color"
app:itemTextColor="@drawable/nav_item_color"
android:background="?colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
app_bar_main_container.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainContainerActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_toolbar"
android:theme="@style/AppNightTheme"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main_container"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
上面的步骤确实有效,但需要我手动设置主题 开始并且不能在运行时更改。我做了一些研究和一些 说只是改变父母的主题确实改变了一切 他们的孩子,但我的不行。
我在 MainContainerActivity.kt 上的尝试
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedPreferences = getSharedPreferences("preferences", 0)
if(sharedPreferences.getBoolean("DayTheme",true)){
setTheme(R.style.AppDayTheme)
}
else{
setTheme(R.style.AppNightTheme)
}
setContentView(R.layout.activity_main_container)
toolbar = findViewById(R.id.toolbar)
toolbar.title = ""
setSupportActionBar(toolbar)
homeFragment = HomeFragment()
discoverFragment = DiscoverFragment()
notificationFragment = NotificationFragment()
settingFragment = SettingFragment()
if(savedInstanceState != null){
when(savedInstanceState.getInt("last_fragment")){
R.id.navigation_discover -> {
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, discoverFragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
R.id.navigation_home -> {
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, homeFragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
R.id.navigation_favorites -> {
}
R.id.navigation_notifications -> {
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, notificationFragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
R.id.navigation_settings -> {
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, settingFragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
else -> false
}
}
else{
val fragmentTransaction = fragmentManager.beginTransaction()
val homeFragment = HomeFragment()
fragmentTransaction.replace(R.id.container,homeFragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
val bottomNav : BottomNavigationView = findViewById(R.id.bottom_nav_view)
bottomNav.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val toggle = ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navView.setNavigationItemSelectedListener(this)
}
【问题讨论】:
标签: android kotlin material-components-android