【问题标题】:How to change Toolbar and BottomNavigationBar theme in activity如何在活动中更改 Toolbar 和 BottomNavigationBar 主题
【发布时间】: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


    【解决方案1】:

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

    至于问题,我很确定您在更改主题后需要重新开始活动。对于 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();
    

    【讨论】:

    • 是的兄弟我已经这样做了,对于里面的片段,颜色已经改变了,但是对于工具栏和底部导航,它没有改变
    • 我只是研究了一下,更好地分析了你的代码。您不能以编程方式更改工具栏和 BottomNavigationView。您显示的代码正在更改上下文主题,而不是“容器”或工具栏主题。也就是说,您连续 3 次对同一活动执行“setTheme”。您只能在运行时更改背景颜色和标题颜色
    【解决方案2】:

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

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

    【讨论】:

    • 是的兄弟,里面的片段根据主题改变了,但底部导航和工具栏的颜色没有改变
    • colorPrimary 在您的样式中将设置为工具栏和底部导航栏颜色
    【解决方案3】:

    你可以在 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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2012-03-20
      相关资源
      最近更新 更多