how to Custom Button (has two TextFields) on Android

前端 未结 2 1408
有刺的猬
有刺的猬 2021-01-07 07:26

I need to develop a button that has two label in.

I find some good articles about custom views, but I can\'t imagine that how can I create a myButton Class(with cus

2条回答
  •  庸人自扰
    2021-01-07 07:36

    I writed this like,.. I have a layout problem. I cant fill screen with two buttons. parentlayout fills screen, but I cant these two buttons put should be..

    enter image description here

    My button layout:

    
            
            
    
    

    And its class:

    public XButton2(Context context, AttributeSet attrs) {
            super(context, attrs);
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = layoutInflater.inflate(R.layout.xbutton2, this);
            icon = (ImageView) view.findViewById(R.id.xbutton2_icon);
            tv = (TextView) view.findViewById(R.id.xbutton2_tv);
            init(attrs);
        }
        protected void init(AttributeSet attrs) {
            parseAttributes(attrs);
            setAttrs();
        }
        protected void parseAttributes(AttributeSet attrs) {
            TypedArray param = getContext().obtainStyledAttributes(attrs,
                    R.styleable.com_matriksdata_bavul_XButton2);
            this.text = param
                    .getString(R.styleable.com_matriksdata_bavul_XButton2_text);
            String str = param
                    .getString(R.styleable.com_matriksdata_bavul_XButton2_icon);
    
            if (str != null) {
                String[] arr = str.split("\\/");
                this.iconResorucesID = getResources().getIdentifier(
                        getContext().getApplicationContext().getPackageName() + ":"
                                + arr[arr.length - 2] + "/"
                                + arr[arr.length - 1].split("\\.")[0], null, null);
            }
            this.textSize = param.getFloat(
                    R.styleable.com_matriksdata_bavul_XButton2_textSize, 40);
    
            param.recycle();
        }
    
        protected void setAttrs() {
            if (text != null) {
                tv.setText(text);
                tv.setTextSize(XUtil.convertToPixcell(getContext(), textSize));
                // tv.setTextColor(textColor);
                // tv.setHighlightColor(textSelectedColor);
            }
            if (iconResorucesID != 0)
                icon.setImageResource(iconResorucesID);
    
        }
    
        public void setChecked(boolean isChecked) {
            if (isChecked) {
                // setBackgroundResource(selectedBg);
                tv.setSelected(true);
            } else {
                tv.setSelected(false);
                // setBackgroundResource(bg);
            }
            this.isChecked = isChecked;
        }
    

    And it is where I used it.

    
                                
    
                                
                        
    

提交回复
热议问题