You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中Activity创建并绘制视图后,如何通过代码切换Toolbar样式?

How to Modify Toolbar Styles (Initial & Dynamic Switching)

Got it, let's break down how to adjust your Toolbar's background and title color—both when your Activity first loads, and later after the view is already drawn. Here's a straightforward, code-focused guide:

1. Initial Setup: Change Styles on Activity Creation

First, make sure your layout includes a Toolbar. Then, you can override its default colors right in the onCreate method.

Layout XML Example

<androidx.appcompat.widget.Toolbar
    android:id="@+id/mainToolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/default_dark_bg" <!-- Your original dark background -->
    android:titleTextColor="@color/default_light_title" /> <!-- Original light title -->

Kotlin Code for Initial Styling

In your Activity's onCreate:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val toolbar = findViewById<Toolbar>(R.id.mainToolbar)
    setSupportActionBar(toolbar) // Treat Toolbar as the ActionBar (optional but common)

    // Update background color
    toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.new_light_bg))
    // Update title text color
    toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.new_dark_title))
    // Optional: Adjust subtitle color if you use one
    toolbar.setSubtitleTextColor(ContextCompat.getColor(this, R.color.new_subtitle_color))
}

Java Equivalent

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.mainToolbar);
    setSupportActionBar(toolbar);

    // Update background color
    toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.new_light_bg));
    // Update title text color
    toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.new_dark_title));
    // Optional: Subtitle color
    toolbar.setSubtitleTextColor(ContextCompat.getColor(this, R.color.new_subtitle_color));
}

2. Dynamic Style Switching After View is Drawn

To change the Toolbar's style after your Activity has already created and drawn its views (like on a button click or user action), you can reuse the same color-setting methods anywhere in your Activity.

Example: Toggle Styles on Button Click

Kotlin

// Assume you have a button with id btnToggleToolbarStyle
val toggleButton = findViewById<Button>(R.id.btnToggleToolbarStyle)
toggleButton.setOnClickListener {
    val toolbar = findViewById<Toolbar>(R.id.mainToolbar)
    
    // Check current style to toggle between two states
    val isCurrentDark = toolbar.backgroundColor == ContextCompat.getColor(this, R.color.default_dark_bg)
    if (isCurrentDark) {
        // Switch to light style
        toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.new_light_bg))
        toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.new_dark_title))
        // Optional: Match status bar color to Toolbar background
        window.statusBarColor = ContextCompat.getColor(this, R.color.light_bg_dark_shade)
    } else {
        // Switch back to dark style
        toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.default_dark_bg))
        toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.default_light_title))
        window.statusBarColor = ContextCompat.getColor(this, R.color.dark_bg_dark_shade)
    }
}

Java

Button toggleButton = findViewById(R.id.btnToggleToolbarStyle);
toggleButton.setOnClickListener(v -> {
    Toolbar toolbar = findViewById(R.id.mainToolbar);
    
    boolean isCurrentDark = toolbar.getBackgroundColor() == ContextCompat.getColor(this, R.color.default_dark_bg);
    if (isCurrentDark) {
        // Switch to light style
        toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.new_light_bg));
        toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.new_dark_title));
        // Optional: Update status bar
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.light_bg_dark_shade));
    } else {
        // Switch back to dark style
        toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.default_dark_bg));
        toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.default_light_title));
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.dark_bg_dark_shade));
    }
});

Key Tips

  • Use ContextCompat.getColor() instead of direct resource calls to support older Android versions (pre-API 23).
  • If you want more complex styling (like font changes or elevation), you can define a Toolbar theme in XML and apply it dynamically with toolbar.setTheme(R.style.MyCustomToolbarTheme).
  • Don’t skip setSupportActionBar(toolbar) if you want the Toolbar to handle ActionBar features like menu items or up navigation.

内容的提问来源于stack exchange,提问作者Jerin Babu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:57:58