navasmdc / MaterialDesignLibrary

This is a library with components of Android L to you use in android 2.2
Apache License 2.0
8.99k stars 2.22k forks source link

ButtonRectangle not worked with style #60

Open Anth06ny opened 9 years ago

Anth06ny commented 9 years ago

Hi thanks for your librairie

This is working <com.gc.materialdesign.views.ButtonRectangle style="@style/button_rounded_main" android:background="@color/strong_yellow" android:text="@string/connexion"/>

This is not working :

<com.gc.materialdesign.views.ButtonRectangle style="@style/button_rounded_main" android:text="@string/connexion"/>

This is not working : <com.gc.materialdesign.views.ButtonRectangle style="@style/button_rounded_main" android:background="?color_composant_main" android:text="@string/connexion"/>

Thank you for you library

kaleai commented 9 years ago

ButtonRectangle? show you code please.

kaleai commented 9 years ago

Can you paste your button_rounded_main.xml here.

Anth06ny commented 9 years ago

I don't know why, github change my text

capture

kaleai commented 9 years ago

Ok,wait a minute

kaleai commented 9 years ago

I found the same probrem. @style/xxxx don't work

kaleai commented 9 years ago

public class ButtonRectangle extends Button {

protected TextView textButton;
protected int defaultTextColor;

public ButtonRectangle(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onInitDefaultValues(){
    super.onInitDefaultValues();
    textButton = new TextView(getContext());
    defaultTextColor = Color.WHITE;
    rippleSpeed = 5.5f;
    minWidth = 80;
    minHeight = 36;
    backgroundResId = R.drawable.background_button_rectangle;
}

@Override
protected void onInitAttributes(AttributeSet attrs) {
    super.onInitAttributes(attrs);
    if (isInEditMode()) {
        // 为了在编译器中预览时不报空指针,在这里产生一个textView对象。实际中不会产生的。
        textButton = new TextView(getContext());
    }
    String text = null;
    /**
     * 设置按钮上的文字内容
     */
    int textResource = attrs.getAttributeResourceValue(ANDROIDXML,"text",-1);
    if(textResource != -1){
        text = getResources().getString(textResource);
    }else{
        //如果没有文字资源,也就是@String/xx,那么就设置文字
        text = attrs.getAttributeValue(ANDROIDXML,"text");
    }

    /**
     * 当文字不为空的时候,TextView设置文字,否则不设置文字
     */
    if(text != null){
        textButton.setText(text);
    }

    /**
     * 设置textSize
     */
    String textSize = attrs.getAttributeValue(ANDROIDXML,"textSize");
    if (text != null && textSize != null) {
        textSize = textSize.substring(0, textSize.length() - 2);//12sp->12
        textButton.setTextSize(Float.parseFloat(textSize));
    }

    /**
     * 设置textColor
     */
    int textColor = attrs.getAttributeResourceValue(ANDROIDXML,"textColor",-1);
    if(text != null && textColor != -1){
        textButton.setTextColor(getResources().getColor(textColor));
    }
    else if(text != null ){
        // 16进制的color
        String color = attrs.getAttributeValue(ANDROIDXML,"textColor");
        if(color != null && !isInEditMode()) {
            textButton.setTextColor(Color.parseColor(color));
        }else {
            textButton.setTextColor(defaultTextColor);
        }
    }
    textButton.setTypeface(null, Typeface.BOLD);
    //textButton.setPadding(5, 5, 5, 5);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    params.setMargins(Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()));
    textButton.setLayoutParams(params);
    addView(textButton);

}
kaleai commented 9 years ago

I suggest to use @color/xxx or #xxxx to set the background.

Anth06ny commented 9 years ago

I can't. My application have multiple theme.

kaleai commented 9 years ago

I'm sorry.Maybe you can change the code in CustomView.class.

     protected void setBackgroundAttributes(AttributeSet attrs) {
    int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,"background",-1);
    if(bacgroundColor != -1){
        setBackgroundColor(getResources().getColor(bacgroundColor));
    }else{
        // Color by hexadecimal
        int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
        if(background != -1 && !isInEditMode()) {
            setBackgroundColor(background);
        }else {
            setBackgroundColor(backgroundColor);
        }
    }
}
Anth06ny commented 9 years ago

Same problem with switch composant.

To use theme : On the onCreate methode toolbar_switch_bluetooth.setBackgroundColor(Utils.getColorFromTheme(getActivity(), R.attr.color_composant_main));

public static int getColorFromTheme(Context context, int attr_id){
    TypedValue typedValue = new TypedValue();
    context.getTheme().resolveAttribute(attr_id, typedValue, true);
    return typedValue.data;
}

With this method it's working

edBaev commented 9 years ago

+1 the same problem

Aenterhy commented 9 years ago

+1 the same issue