android.R.attr.colorPrimary总是给我错误的颜色

android.R.attr.colorPrimary总是给我错误的颜色,android,Android,我正在尝试根据当前主题属性为我的按钮提供颜色:android.R.attr.colorPrimary或R.attr.colorPrimary,它们应该返回黄色,但始终为我提供蓝色!我也在清单中设置了主题 例如,使用android:background=“?attr/colorPrimary”设置工具栏的backgroundcolor会给出正确的颜色,但如果它是从代码设置的,则不会给出正确的颜色 这就是我试图设置按钮颜色的方式: TypedValue typedValue = new Ty

我正在尝试根据当前主题属性为我的按钮提供颜色:
android.R.attr.colorPrimary
R.attr.colorPrimary
,它们应该返回黄色,但始终为我提供蓝色!我也在清单中设置了主题

例如,使用
android:background=“?attr/colorPrimary”
设置工具栏的backgroundcolor会给出正确的颜色,但如果它是从代码设置的,则不会给出正确的颜色

这就是我试图设置按钮颜色的方式:

    TypedValue typedValue = new TypedValue();
    App.getAppContex().getTheme().resolveAttribute(android.R.attr.colorPrimary, typedValue, true);
    buttonColor = typedValue.data;


    addButton.setText("SAVE");
    addButton.getBackground().setColorFilter(buttonColor, PorterDuff.Mode.MULTIPLY);
这是我的“黄色”主题

  <style name="AppTheme_Yellow" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primaryYellow</item>
    <item name="colorPrimaryDark">@color/primary_darkYellow</item>
    <item name="colorAccent">@color/accentYellow</item>
    <item name="android:textColorPrimary">@color/primary_textYellow</item>
    <item name="android:textColorSecondary">@color/secondary_textYellow</item>
    <item name="android:icon">@color/iconsYellow</item>
    <item name="actionOverflowButtonStyle">@style/OverFlowStyle</item>
    <item name="popupMenuStyle">@style/popupMenuStyle</item>

@颜色/原黄
@颜色/原色深黄色
@颜色/黄色
@颜色/主文本黄色
@颜色/辅助文本黄色
@颜色/图标黄色
@样式/溢出样式
@样式/弹出菜单样式
黄色主题背后的颜色:

<color name="primaryYellow">#FFC107</color>
<color name="primary_darkYellow">#FFA000</color>
<color name="primary_lightYellow">#FFECB3</color>
<color name="accentYellow">#607D8B</color>
<color name="primary_textYellow">#212121</color>
<color name="secondary_textYellow">#727272</color>
<color name="iconsYellow">#212121</color>
<color name="dividerYellow">#B6B6B6</color>
#FFC107
#FFA000
#FFECB3
#607D8B
#212121
#727272
#212121
#B6B6B6

我在使用的全局应用程序上下文中发现了问题。
App.getAppContex().getTheme().resolveAttribute(android.R.attr.colorPrimary,typedValue,true)

因此,必须使用getActivity()或UI Wigdets在其中被初始化的活动上下文:

getActivity().getTheme().resolveAttribute(android.R.attr.colorPrimary, typedValue, true); 

您在哪里设置这些值?在DialogFragment类中,它包含我刚刚解决的ButtonnerEnd!我使用了错误的上下文!我会发布答案。@AkashBhaveGreat,很高兴你找到了!