activity 如何更改 Toolbar 和 BottomNavigationBar 主题

How to change Toolbar and BottomNavigationBar theme in activity

So, I tried creating bottom navigation bar and navigation drawer in 1 activity (i used new activity drawer template and combine it with the Bottom Navigation View tag I got from bottom navigation activity). I want to change the theme for both the toolbar and bottom navigation drawer on run time ( so I thought about using setTheme on activity) but the setTheme does not work (the color does not change) I tried to set the theme on each attribute on XML (on AppBarLayout and BottomNavigationView container tag) and it does work. But when I tried via programmatically, I set activity on their context, but it does nothing

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>

the step above does work, but it needs me to set the theme manually at the start and cannot change at run time. I did some research and some say that just changing the theme on parent does change everything on their children, but mine does not work.

what I have tried on 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)
    }

首先,您好,欢迎来到 Whosebug!

至于这个问题,我很确定您在更改主题后需要重新启动 activity。对于 Api 11 岁及以后:

Activity.recreate()

11 点前 Api:

Intent intent = getIntent();
finish();
startActivity(intent);

希望对您有所帮助!

编辑 您不能在运行时更改 ToolBar 和 BottomNavigationView 主题。您只能更改背景颜色和标题颜色。 对于工具栏,

toolbar.setBackgroundColor(newColor); 
toolbar.setTitleTextColor(titleColor);

对于 BottomNavigationView,

navigation.setBackgroundColor();
navigation.setItemTextColor();

要在 activity 中以编程方式设置,您必须在 setContentView() 之前设置它。但是如果您想在 activity 启动后更改它,则必须重新创建或重新启动 activity .

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppDayTheme); // (for Custom theme)
    setContentView(R.layout.your_activity);
}

您可以使用 attrs.xml 中的属性标签在颜色标签中使用。请记住,如果您有两个必须是默认主题和次要主题,那么您必须使用相同的属性标签声明两个颜色标签 像这样

第一步:定义属性标签

<attr name="toolbarcolor" format="color"></attr>

使用属性标签定义两种颜色选择

第 2 步:定义颜色名称

 <color name="toolbarcolordefualt">#F36C63</color>
<color name="toolbarcolorsecondary">#4CAF50</color>

第 3 步:定义样式

 <style name="AppTheme.Default" parent="Theme.AppCompat.Light.NoActionBar">
   <item name="toolbarcolor">@color/toolbarcolordefualt</item>
 </style>  

 <style name="AppTheme.secondary" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="toolbarcolor">@color/toolbarcolorsecondary</item>

将该属性用于您的工具栏

<androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:background="?attr/toolbarcolor"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"/>

剩下的事情就是您必须使用 settheme() 函数

中的 sharedpreference 维护需要显示的主题

对于底部导航栏,您可以根据您的共享偏好使用它,使用 if else 条件

getWindow().setNavigationBarColor(getResources().getColor(R.color.anycolor));