【问题标题】:Why do custom crouton Styles appear as grey instead of their specified color?为什么自定义面包圈样式显示为灰色而不是指定的颜色?
【发布时间】:2013-02-21 22:12:27
【问题描述】:

我想为我的应用程序的面包块自定义样式。为尽可能多的样式设置 4 种颜色。这是我的自定义样式类

public class TapabookCroutonStyle {
public static final int DURATION_INFINITE = -1;
public static final Style ALERT;
public static final Style WARN;
public static final Style CONFIRM;
public static final Style INFO;

public static final int AlertRed = R.color.rojo_vivo;
public static final int WarnOrange= R.color.naranja_resplandeciente;
public static final int ConfirmGreen = R.color.verde_lima;
public static final int InfoYellow = R.color.amarillo_canario;

private static final int DURATION_SHORT  = 3000;
private static final int DURATION_MEDIUM = 5000;
private static final int DURATION_LONG   = 10000;


static {
    ALERT   = new Style.Builder()
                .setDuration(DURATION_LONG)
                .setBackgroundColorValue(AlertRed)
                .setHeight(LayoutParams.WRAP_CONTENT)
                .build();
    WARN    = new Style.Builder()
                .setDuration(DURATION_MEDIUM)
                .setBackgroundColorValue(ConfirmGreen)
                .setHeight(LayoutParams.WRAP_CONTENT)
                .build();
    CONFIRM = new Style.Builder()
                .setDuration(DURATION_MEDIUM)
                .setBackgroundColorValue(ConfirmGreen)
                .setHeight(LayoutParams.WRAP_CONTENT)
                .build();
    INFO    = new Style.Builder()
                .setDuration(DURATION_MEDIUM)
                .setBackgroundColorValue(InfoYellow)
                .setHeight(LayoutParams.WRAP_CONTENT)
                .build();
}
}

颜色在 color.xml 文件中设置

<color name="verde_lima">#aaee22</color>
<color name="rojo_vivo">#E8110F</color>
<color name="naranja_resplandeciente">#FF6600</color>
<color name="amarillo_canario">#FFCC00</color>

我使用包装器来调用面包块。

/**             Crouton Wrappers                 **/
public void croutonAlert(int stringId){
    Crouton.makeText(this, stringId, TapabookCroutonStyle.ALERT).show();
}
public void croutonAlert(String text){
    Crouton.makeText(this, text, TapabookCroutonStyle.ALERT).show();
}

public void croutonInfo(int stringId){
    Crouton.makeText(this, stringId, TapabookCroutonStyle.INFO).show();
}
public void croutonInfo(String text){
    Crouton.makeText(this, text, TapabookCroutonStyle.INFO).show();
}

public void croutonConfirm(int stringId){
    Crouton.makeText(this, stringId, TapabookCroutonStyle.CONFIRM).show();
}
public void croutonConfirm(String text){
    Crouton.makeText(this, text, TapabookCroutonStyle.CONFIRM).show();
}
public void croutonWarn(int stringId){
    Crouton.makeText(this, stringId, TapabookCroutonStyle.WARN).show();
}
public void croutonWarn(String text){
    Crouton.makeText(this, text, TapabookCroutonStyle.WARN).show();
}

因为我使用的是 ActionBarSherlock,所以我的 appTheme 继承自它而不是 holo。在另一个使用标准面包片的应用程序上,它没有任何问题。但是,此处不会显示自定义油炸面包丁。我在 2.2 自定义 ROM 和 4.2(google 版本)上对其进行了测试。

我发现的关于这个主题的唯一问题是Holo Colors on pre Holo Devices?,它不处理自定义样式(与我的情况不同,该问题不会在“全息设备”上重现)。

有谁知道为什么这四种样式显示为灰色?

编辑:我刚刚测试过像 Style.ALERT 这样的常规(内置)样式确实显示了正确的颜色...... 此外,我将颜色引用从 R.color.mycolor 更改为它们在 R 中的值(例如:0x7f06000c),因为这就是 Crouton 库中的原始样式类是如何做到的,并且仍然是相同的半透明灰色...... 我还检查了原始的 holo_red_light 以检查 alfa 值并将它们添加到我的自定义颜色中

<color name="verde_lima">#FFaaee22</color>
<color name="rojo_vivo">#FFE8110F</color>
<color name="naranja_resplandeciente">#FFFF6600</color>
<color name="amarillo_canario">#FFFFCC00</color>

但还是什么都没有。

【问题讨论】:

    标签: android customization crouton


    【解决方案1】:

    您正在使用setBackgroundColorValue(...) 方法,它需要一个实际的颜色值。 但是您正在为此方法提供资源 ID。

    您可能想调用 setBackgroundColor(int resId),它会在内部解析资源 ID。

    【讨论】:

    • 成功了!我使用 setBackgroundColorValue 从original Style class 复制它,所以我仍然不明白为什么它适用于它而不适用于我。但它有效,这才是最重要的:)
    • 原来的 Style 类使用 color values 而不是 resource id's ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    相关资源
    最近更新 更多