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

如何在ListView中添加深色模式手动切换选项?

实现ListView中手动切换深色主题的简易方案

你有一个展示应用技术细节的ListView,希望实现手动深色主题切换功能,现有ListView的点击事件代码如下:

aboutListView.setOnItemClickListener((parent, view, i, id) -> {
    if (i == 0) {
       ConnectivityManager connectivityManager1 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo1 = connectivityManager1.getActiveNetworkInfo();
        if (networkInfo1 == null || !networkInfo1.isConnected() || !networkInfo1.isAvailable()) {
            Intent intent = new Intent(MainList.this, no_network.class);
            startActivity(intent);
            finish();

        } else {
            String url = "https://example.com/";
            CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder()
                    .setShareState(CustomTabsIntent.SHARE_STATE_OFF);
            CustomTabsIntent customTabsIntent = builder.build();
            customTabsIntent.launchUrl(this, Uri.parse(url));
        }

    } else if (i == 1) {
        ConnectivityManager connectivityManager1 = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo1 = connectivityManager1.getActiveNetworkInfo();
        if (networkInfo1 == null || !networkInfo1.isConnected() || !networkInfo1.isAvailable()) {
            Intent intent = new Intent(MainList.this, no_network.class);
            startActivity(intent);
            finish();
        } else {
            String url = "https://example.com/";
            CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder()
                    .setShareState(CustomTabsIntent.SHARE_STATE_OFF);
            CustomTabsIntent customTabsIntent = builder.build();
            customTabsIntent.launchUrl(this, Uri.parse(url));
        }
    }

});

以下是简易实现方法:

1. 准备主题样式

在项目的res/values/styles.xml和res/values-night/styles.xml中分别定义浅色、深色主题的基础样式(比如背景色、文字颜色),确保你的Activity使用这些主题:

<!-- res/values/styles.xml -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="android:background">@color/light_bg</item>
    <item name="android:textColor">@color/light_text</item>
</style>

<!-- res/values-night/styles.xml -->
<style name="AppTheme" parent="Theme.MaterialComponents.Dark.NoActionBar">
    <item name="android:background">@color/dark_bg</item>
    <item name="android:textColor">@color/dark_text</item>
</style>

2. 在ListView点击事件中添加主题切换逻辑

假设主题切换是ListView的第3个选项(对应i == 2),在现有点击事件中新增分支:

else if (i == 2) {
    // 读取当前主题状态
    SharedPreferences sp = getSharedPreferences("AppSettings", MODE_PRIVATE);
    boolean isDarkMode = sp.getBoolean("isDarkMode", false);
    
    // 切换夜间模式
    getDelegate().setLocalNightMode(isDarkMode ? AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES);
    
    // 保存新的主题状态
    sp.edit().putBoolean("isDarkMode", !isDarkMode).apply();
    
    // 重启当前Activity使主题生效
    recreate();
}

3. 启动时恢复主题状态

在当前Activity的onCreate方法开头,先读取保存的主题状态并设置:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // 先恢复主题设置
    SharedPreferences sp = getSharedPreferences("AppSettings", MODE_PRIVATE);
    boolean isDarkMode = sp.getBoolean("isDarkMode", false);
    getDelegate().setLocalNightMode(isDarkMode ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_list);
    
    // 初始化ListView等其他代码...
}

4. 优化现有重复代码(可选)

你现有代码中网络判断逻辑重复,可抽成工具方法简化代码:

private boolean isNetworkConnected() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnected() && networkInfo.isAvailable();
}

之后点击事件里的网络判断可以替换成:

if (i == 0) {
    if (!isNetworkConnected()) {
        startActivity(new Intent(MainList.this, no_network.class));
        finish();
    } else {
        // 打开链接逻辑
        String url = "https://example.com/";
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder()
                .setShareState(CustomTabsIntent.SHARE_STATE_OFF);
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.launchUrl(this, Uri.parse(url));
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 02:45:20