自定义组合控件

这个控件可作为表单的item。可以自定义左右两边的文字颜色和大小。
先看效果

自定义组合控件_第1张图片
device-2017-04-07-112342.png

1.布局文件
根布局使用merge减少层级

  


    

        

        

        
    

    

2.继承相对布局

public class ItemChooseView extends RelativeLayout {
    private final static int DEFAULT_LEFT_TEXTSIZE=15;
    private final static int DEFAULT_RIGHT_TEXTSIZE=15;
    private final static int DEFAULT_LEFT_COLOR=Color.BLACK;
    private final static int DEFAULT_RIGHT_COLOR=Color.BLUE;
    private CharSequence textLeft;
    private CharSequence textRigth;
    private int textLeftColor;
    private int textRightColor;
    private float textLeftTextSize;
    private float textRightTextSize;
    private TextView textviewLeft;
    private TextView textviewRight;

    public ItemChooseView(@NonNull Context context) {
        this(context,null);
    }

    public ItemChooseView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ItemChooseView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(getContext(), R.layout.itemchoose_layout, this);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ItemChooseView);
        //左边文字
        textLeft = array.getText(R.styleable.ItemChooseView_textLeft);
        //右边文字
        textRigth = array.getText(R.styleable.ItemChooseView_textRight);
        //左边文字颜色
        textLeftColor = array.getColor(R.styleable.ItemChooseView_textLeftColor,
                DEFAULT_LEFT_COLOR);
        //右边文字颜色
        textRightColor = array.getColor(R.styleable.ItemChooseView_textRightColor,
                DEFAULT_RIGHT_COLOR);
        //左边文字大小
        textLeftTextSize = array.getDimension(R.styleable.ItemChooseView_textLeftTextSize,
                DEFAULT_LEFT_TEXTSIZE);
        //右边文字大小
        textRightTextSize = array.getDimension(R.styleable.ItemChooseView_textRightTextSize,
                DEFAULT_RIGHT_TEXTSIZE);
        array.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        textviewLeft = (TextView) findViewById(R.id.tvLeft_itemchooseview);
        textviewRight = (TextView) findViewById(R.id.tvRight_itemchooseview);
        textviewLeft.setText(textLeft);
        if(textLeftTextSize==DEFAULT_LEFT_TEXTSIZE) {
            textviewLeft.setTextSize(textLeftTextSize);
        }else{
            textviewLeft.getPaint().setTextSize(textLeftTextSize);
        }
        textviewLeft.setTextColor(textLeftColor);

        textviewRight.setText(textRigth);
        if(textRightTextSize==DEFAULT_RIGHT_TEXTSIZE) {
            textviewRight.setTextSize(textRightTextSize);
        }else{
            textviewRight.getPaint().setTextSize(textRightTextSize);
        }
        textviewRight.setTextColor(textRightColor);
    }
    public void setTextviewLeft(CharSequence charSequence){
        textviewLeft.setText(charSequence);
    }
    public CharSequence getTextViewLeft(){
        return textviewLeft.getText();
    }
    public void setTextViewRight(CharSequence charSequence){
        textviewRight.setText(charSequence);
    }
    public CharSequence getTextViewRight(){
        return textviewRight.getText();
    }
    public void setTextviewLeftColor(int color){
        textviewLeft.setTextColor(color);
    }
    public void setTextviewRightColor(int color){
        textviewRight.setTextColor(color);
    }
    public void setTextviewLeftTextSize(float textsize){
        textviewLeft.setTextSize(textsize);
    }
    public void setTextviewRightTextSize(float textsize){
        textviewRight.setTextSize(textsize);
    }
}

3.在布局文件中使用
运行在真机上如上图所示

  


    
    
    

你可能感兴趣的:(自定义组合控件)