5

我想为我的应用程序的面包块自定义样式。为尽可能多的样式设置 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(谷歌版本)上对其进行了测试。

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

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

编辑:我刚刚测试过像 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>

但仍然没有。

4

1 回答 1

3

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

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

于 2013-02-22T10:39:16.033 回答