自定义控件

来源:互联网 发布:java版波斯王子2下载 编辑:程序博客网 时间:2024/05/21 17:55
<span style="font-family:Comic Sans MS;font-size:18px;">import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.widget.*;/** * Created by Administrator on 15-2-4. */public class TitleView extends RelativeLayout implements View.OnClickListener{    private LeftOnClickListener msetLeftOnClickListener;    private RightOnClickListener msetrightOnClickListener;    private Button leftbtn,rightbtn;    private TextView textView;    private String lefttext,righttext,text;    private int leftTextSize,rightTextSize,textSize,leftTextColor,rightTextColor,textColor,                leftbg,rightbg;    public TitleView(Context context) {        super(context);        init();    }    public TitleView(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.TitleView);         lefttext=a.getString(R.styleable.TitleView_left_text);         righttext=a.getString(R.styleable.TitleView_right_text);         text=a.getString(R.styleable.TitleView_center_text);         leftTextSize=a.getDimensionPixelSize(R.styleable.TitleView_left_textsize,0);         rightTextSize=a.getDimensionPixelSize(R.styleable.TitleView_right_textsize,0);         textSize=a.getDimensionPixelSize(R.styleable.TitleView_center_textsize,0);         leftTextColor=a.getColor(R.styleable.TitleView_left_textcolor,0);         rightTextColor=a.getColor(R.styleable.TitleView_right_textcolor,0);         textColor=a.getColor(R.styleable.TitleView_center_textcolor,0);         leftbg=a.getResourceId(R.styleable.TitleView_left_bg,0);         rightbg=a.getResourceId(R.styleable.TitleView_right_bg,0);        init();    }    public TitleView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        init();    }    private void init(){        LayoutInflater.from(getContext()).inflate(R.layout.view_titleview_layout,this);        leftbtn=(Button)findViewById(R.id.leftbtn);        rightbtn=(Button)findViewById(R.id.rightbtn);        textView=(TextView)findViewById(R.id.textview);        leftbtn.setOnClickListener(this);        rightbtn.setOnClickListener(this);        if(lefttext!=null){          setLeftText(lefttext);        }        if(righttext!=null){           setRightText(righttext);        }        if(text!=null){           setCenterText(text);        }        if(leftTextSize!=0){            setLeftTextSize(leftTextSize);        }        if(rightTextSize!=0){            setRightTextSize(rightTextSize);        }        if(textSize!=0){            setTextSize(textSize);        }        if(leftTextColor!=0){            setLeftTextColor(leftTextColor);        }        if(rightTextColor!=0){            setRightTextColor(leftTextColor);        }        if(textColor!=0){            setTextColor(leftTextColor);        }        if(leftbg!=0){            setLeftbg(leftbg);        }        if(rightbg!=0){            setRightbg(rightbg);        }    }    public void setLeftText(String s){        leftbtn.setText(s);    }    public void setRightText(String s){        rightbtn.setText(s);    }    public void setCenterText(String s){        textView.setText(s);    }    public void setLeftTextSize(int dimension){        leftbtn.setTextSize(dimension);    }    public void setRightTextSize(int dimension){        rightbtn.setTextSize(dimension);    }    public void setTextSize(int dimension){        textView.setTextSize(dimension);    }    public void setLeftTextColor(int color){        leftbtn.setTextColor(color);    }    public void setRightTextColor(int color){        rightbtn.setTextColor(color);    }    public void setTextColor(int color){        textView.setTextColor(color);    }    public void setLeftbg(int drowable){        leftbtn.setBackgroundResource(drowable);    }    public void setRightbg(int drowable){        rightbtn.setBackgroundResource(drowable);    }    public void setLeftOnClickListener(LeftOnClickListener  clickListener){            msetLeftOnClickListener=clickListener;    }    public void setRightOnClickListener(RightOnClickListener  clickListener){            msetrightOnClickListener=clickListener;    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.leftbtn:              msetLeftOnClickListener.onClick();                break;            case R.id.rightbtn:              msetrightOnClickListener.onClick();                break;        }    }    interface LeftOnClickListener{        public void onClick();    }    interface RightOnClickListener{        public void onClick();    }}</span>
<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="TitleView">        <attr name="left_text" format="string"></attr>        <attr name="center_text" format="string"></attr>        <attr name="right_text" format="string"></attr>        <attr name="left_textsize" format="dimension"></attr>        <attr name="center_textsize" format="dimension"></attr>        <attr name="right_textsize" format="dimension"></attr>        <attr name="left_textcolor" format="color"></attr>        <attr name="center_textcolor" format="color"></attr>        <attr name="right_textcolor" format="color"></attr>        <attr name="left_bg" format="reference"></attr>        <attr name="center_bg" format="reference"></attr>        <attr name="right_bg" format="reference"></attr>        <attr name="background" format="color"></attr>        <attr name="backgroundDrawble" format="reference"></attr>        <attr name="width" format="dimension"></attr>        <attr name="heigth" format="dimension"></attr>    </declare-styleable></resources></span>


0 0
原创粉丝点击