SwitchPreference默认颜色

时间:2015-06-05 20:04:30

标签: java android xml

我正在处理的应用程序我已将主要/暗/重点颜色设置为我想要的颜色,并且它们出现在正确的位置(如预期的那样)。我有一个我正在使用的偏好活动,我希望我使用的preferenceswitch的颜色会以强调颜色呈现。相反,它们呈现材料蓝绿色。我想知道这是Lollipop的默认行为,就像Kitkat那样是蓝色的吗?我甚至不会在我的代码或我的#009688 / colors.xml中引用styles.xml的颜色。

colors.xml

<resources>
    <color name="primary">#00BCD4</color>
    <color name="primary_dark">#0097A7</color>
    <color name="accent">#FFD740</color>
</resources>

styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimary">@color/primary</item>
        <item name="android:colorPrimaryDark">@color/primary_dark</item>
        <item name="android:colorAccent">@color/accent</item>
    </style>
</resources>

任何想法?我将提供更多信息。我在这里看到了一些关于创建自定义内容的东西,但这真的有必要吗?

preferenceActivity.java

public class PreferenceActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PrefFrag prefFragment = new PrefFrag();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(android.R.id.content, prefFragment);
        fragmentTransaction.commit();
    }
}

1 个答案:

答案 0 :(得分:6)

当您使用AppCompat时,您应该使用每个属性的非前缀版本 - 这可以确保它们在所有API级别上都可用(与android:级别不同,后者仅适用于API21 +示例):

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>