【问题标题】:Custom android button with image带有图像的自定义 android 按钮
【发布时间】:2016-08-01 12:35:57
【问题描述】:

我的切换按钮有问题。我正在尝试从库navasmdc 扩展 ButtonRectangle,并且我想添加图像。像 ImageButton 这样具有涟漪效果的东西。

我尝试设置背景可绘制,但它没有我想要的效果。

我尝试制作自己的切换按钮,但这种涟漪效应超出了我的技能。

所以我问。

  1. 是否可以扩展布局并添加ImageView?
  2. 如果是这样,它是如何完成的?

这是我的代码,到目前为止:

public class StyleableToggleButton extends ButtonRectangle implements StyleableView, Checkable {



private boolean checked = false;
private int primaryColor = Config.DEFAULT_PRIMARY_COLOR;
private int secondaryColor = Config.DEFAULT_SECONDARY_COLOR;
private float rippleSpeed = 18f;


public StyleableToggleButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    applyStyle();
    setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            toggle();
            applyStyle();
        }
    });
    setRippleSpeed(rippleSpeed);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        setBackgroundDrawable(getResources().getDrawable(R.drawable.direction_in_w));
    }
}

@Override
public void setChecked(boolean b) {
    checked = b;
    applyStyle();
}

@Override
public boolean isChecked() {
    return checked;
}

@Override
public void toggle() {
    checked = !checked;
    applyStyle();
}

@Override
public void applyStyle() {

    if (checked){
        this.setBackgroundColor(primaryColor);
    }else {
        this.setBackgroundColor(secondaryColor);
    }
}

@Override
protected int makePressColor(){
    return !checked ? primaryColor : secondaryColor;
}
}

【问题讨论】:

    标签: android button togglebutton


    【解决方案1】:

    我后来找到了答案。我仍然不确定问题 1。但您可以通过编程方式对其进行扩展。

    2.问题的答案是addView(View v)

    所以我的代码如下所示:

    public class StyleableToggleButton extends ButtonRectangle implements    StyleableView, Checkable {
    
    
    
    private boolean checked = false;
    private int primaryColor = Config.DEFAULT_PRIMARY_COLOR;
    private int secondaryColor = Config.DEFAULT_SECONDARY_COLOR;
    private float rippleSpeed = 18f;
    private TextView textView;
    private ImageView imageView;
    private LinearLayout linearLayout;
    
    private int minHeight;
    private float fontSize = 16f;
    
    
    public StyleableToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        minHeight = (getResources().getDisplayMetrics().heightPixels - AttUtils.dpToPx(getResources().getDimension(R.dimen.height_att_activity_bar),getResources())) / 4;
    
        setupButton(); 
    
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                toggle();
                applyStyle();
            }
        });
        setRippleSpeed(rippleSpeed);
    
    
    }
    
    
    //This is where we add textView and imageView to existing layout. We just kept previous empty.
    private void setupButton() {
    
        setMinimumHeight(minHeight);
    
        linearLayout = new LinearLayout(getContext());
        linearLayout.setOrientation(LinearLayout.VERTICAL);
    
    
        LinearLayout.LayoutParams layoutParams;
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(layoutParams);
    
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
    
        textView = new TextView(getContext());
        textView.setLayoutParams(layoutParams);
        textView.setTypeface(null, Typeface.BOLD);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            textView.setTextAlignment(TEXT_ALIGNMENT_CENTER);
        }
        textView.setTextSize(fontSize);
    
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
    
        imageView = new ImageView(getContext());
        imageView.setLayoutParams(layoutParams);
    
        linearLayout.addView(imageView);
        linearLayout.addView(textView);
        addView(linearLayout);
    }
    
    @Override
    public void setChecked(boolean b) {
        checked = b;
        applyStyle();
    }
    
    @Override
    public boolean isChecked() {
        return checked;
    }
    
    @Override
    public void toggle() {
        checked = !checked;
        applyStyle();
    }
    
    @Override
    public void applyStyle() {
    
        Drawable dr = imageView.getDrawable();
        if (checked){
            this.setBackgroundColor(primaryColor);
            textView.setTextColor(secondaryColor);
            if (dr != null) {
                dr.setColorFilter(secondaryColor, PorterDuff.Mode.MULTIPLY);
                imageView.setImageDrawable(dr);
            }
        }else {
            this.setBackgroundColor(secondaryColor);
            textView.setTextColor(primaryColor);
            if (dr != null) {
                dr.setColorFilter(primaryColor, PorterDuff.Mode.MULTIPLY);
                imageView.setImageDrawable(dr);
            }
        }
    
    }
    
    @Override
    protected int makePressColor(){
        return !checked ? primaryColor : secondaryColor;
    }
    
    
    @Override
    public void setText(String text){
        textView.setText(text);
    }
    
    public void setImage(Drawable drawable){
        imageView.setImageDrawable(drawable);
    }
    
    public void setLayoutMargin(int marginWidth, int marginHeight){
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginWidth, marginHeight, marginWidth, marginHeight);
        linearLayout.setLayoutParams(layoutParams);
    }
    
    public void setTextViewMargin(int marginWidth, int marginHeight){
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginWidth, marginHeight, marginWidth, marginHeight);
        textView.setLayoutParams(layoutParams);
    }
    
    public void setImageViewMargin(int marginWidth, int marginHeight){
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(marginWidth, marginHeight, marginWidth, marginHeight);
        imageView.setLayoutParams(layoutParams);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多