首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >偏好片段中的图标不适应夜间模式

偏好片段中的图标不适应夜间模式
EN

Stack Overflow用户
提问于 2019-12-06 18:36:15
回答 1查看 2K关注 0票数 4

我正在开发一款安卓应用程序,并想要制作一款SettingsActivity。我的问题是,我的矢量绘图不能适应夜以继日的主题。

ic_palette_black_24dp.xml:

代码语言:javascript
复制
<vector android:height="24dp" android:tint="#FFFFFF"
    android:viewportHeight="24.0" android:viewportWidth="24.0"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@color/icon_color_on_surface" android:pathData="M12,3c-4.97,0 -9,4.03 -9,9s4.03,9 9,9c0.83,0 1.5,-0.67 1.5,-1.5 0,-0.39 -0.15,-0.74 -0.39,-1.01 -0.23,-0.26 -0.38,-0.61 -0.38,-0.99 0,-0.83 0.67,-1.5 1.5,-1.5L16,16c2.76,0 5,-2.24 5,-5 0,-4.42 -4.03,-8 -9,-8zM6.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S5.67,9 6.5,9 8,9.67 8,10.5 7.33,12 6.5,12zM9.5,8C8.67,8 8,7.33 8,6.5S8.67,5 9.5,5s1.5,0.67 1.5,1.5S10.33,8 9.5,8zM14.5,8c-0.83,0 -1.5,-0.67 -1.5,-1.5S13.67,5 14.5,5s1.5,0.67 1.5,1.5S15.33,8 14.5,8zM17.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S16.67,9 17.5,9s1.5,0.67 1.5,1.5 -0.67,1.5 -1.5,1.5z"/>
</vector>

值/colors.xml s.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?> 
<resources>
    <color name="icon_color_on_surface">#000000</color>
</resources>

values night/chros.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="icon_color_on_surface">#FFFFFF</color>
</resources>

在我的root_preferences.xml中:

代码语言:javascript
复制
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root_preferences">
    <PreferenceCategory android:title="@string/title_preference_design">

        <ListPreference
            android:entries="@array/preference_list_theme"
            android:entryValues="@array/preference_list_theme_values"
            android:icon="@drawable/ic_palette_black_24dp"
            android:key="list_preference_theme"
            android:tint="@color/icon_color_on_surface"
            android:tintMode="src_atop"
            android:title="@string/preference_category_theme"
            app:icon="@drawable/ic_palette_black_24dp" />
    </PreferenceCategory>
</PreferenceScreen>

编辑

这里还有我的SettingsActivity.java,它基本上是AndroidStudio的默认设置:

代码语言:javascript
复制
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;

import java.util.ArrayList;

public class SettingsActivity extends AppCompatActivity {

    private ColorHelper colorHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.settings, new SettingsFragment())
                .commit();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

    }

    public static class SettingsFragment extends PreferenceFragmentCompat {

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            // Indicate here the XML resource you created above that holds the preferences
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

        }
    }
}

/EDIT

正如您在root_preferences.xml中看到的,我已经尝试用android:tint=""android:tintMode=""设置它,但是不管我做了什么,图标的颜色都是白色的。在其他活动中,使用android:tint=""的解决方案确实有效。

我希望有人能帮我这个忙。谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-01-19 04:51:24

实现这一目标的方法是为fillColor定义一个资源属性。而且,如果我没有记错的话,您在向量的XML中定义的tint要么是覆盖的,要么对SVG的绘图没有任何影响。我的绘图没有tint颜色集,而且fillColor属性对于所有可绘图都是动态的。

下面,android:fillColor="?android:attr/textColorSecondary"是由Android系统定义的,并且已经由Theme.MaterialComponents.DayNight处理。您还可以创建自己的attr值。

代码语言:javascript
复制
<vector 
    android:height="24dp" 
    android:viewportHeight="24.0" 
    android:viewportWidth="24.0"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <path 
         android:fillColor="?android:attr/textColorSecondary" 
         android:pathData="..."
    />
</vector>

根据您的用例,您还可以使用textColorPrimary、来自?android:attrtextColorTertiary来表示不同的颜色。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59218581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档