xuxiuxi
2017-04-11 3e4714bccc70f4ee12a206c5ee1249fe747c4fa6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package cn.com.basic.face.util;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import com.bsk.zhangbo.demoforbsk.R;
 
/**
 * Created by Sinoe on 2017/2/22.
 */
 
public class InfoTextView extends LinearLayout{
 
 
    private Context mContext;
    private TextView mLeftTextView,mRightTextView;
    private String mLeftTextString,mRightTextString;
    private int mBetweenMargin,mLeftTextSize,mRightTextSize,mLeftTextColor,mRightTextColor,mBackgroundColor;
    private int mDefaultMargin = 0;
    private int mDefaultTextSize = 0;
    private int mDefaultTextColor = 0xff515b64;
    private int mDefaultBackgroundColor = 0xffffffff;
    private LayoutParams mLeftLayoutParams,mRightLayoutParams;
    public InfoTextView(Context context) {
        super(context);
    }
 
    public InfoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttr(attrs);
        initLayout();
    }
 
 
    /**
     * 初始化布局
     */
    private void initLayout(){
        this.setBackgroundColor(mBackgroundColor);
        this.setOrientation(HORIZONTAL);
        initLeftText();
        initRightText();
    }
 
    /**
     * 获取定义属性值
     * @param attrs
     */
    private void getAttr(AttributeSet attrs){
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.VisitorView);
        mLeftTextString = typedArray.getString(R.styleable.VisitorView_vLeftTextString);
        mRightTextString = typedArray.getString(R.styleable.VisitorView_vRightTextString);
        mBetweenMargin = typedArray.getDimensionPixelSize(R.styleable.VisitorView_vBetweenMargin,mDefaultMargin);
        mLeftTextSize = typedArray.getDimensionPixelSize(R.styleable.VisitorView_vLeftTextSize,mDefaultTextSize);
        mRightTextSize = typedArray.getDimensionPixelSize(R.styleable.VisitorView_vRightTextSize,mDefaultTextSize);
        mLeftTextColor = typedArray.getColor(R.styleable.VisitorView_vLeftTextColor,mDefaultTextColor);
        mRightTextColor = typedArray.getColor(R.styleable.VisitorView_vRightTextColor,mDefaultTextColor);
        mBackgroundColor = typedArray.getColor(R.styleable.VisitorView_vBackgroundColor,mDefaultBackgroundColor);
        typedArray.recycle();
    }
 
    /**
     * 初始化左边文字
     */
    private void initLeftText(){
        mLeftTextView = new TextView(mContext);
        mLeftLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mLeftTextView.setLayoutParams(mLeftLayoutParams);
        mLeftTextView.setText(mLeftTextString);
        setTextColor(mLeftTextView,mLeftTextColor);
        setTextSize(mLeftTextView,mLeftTextSize);
        addView(mLeftTextView);
    }
 
    /**
     * 初始化右边文字
     */
    private void initRightText(){
        mRightTextView = new TextView(mContext);
        mRightLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mRightTextView.setLayoutParams(mRightLayoutParams);
        mRightTextView.setText(mRightTextString);
        setTextColor(mRightTextView,mRightTextColor);
        setTextSize(mRightTextView,mRightTextSize);
        addView(mRightTextView);
    }
 
 
    /**
     * 设置字体颜色
     * @param textView  textview对象
     * @param color 文字颜色
     */
    private void setTextColor(TextView textView,int color){
        textView.setTextColor(color);
    }
 
    /**
     * 设置字体大小
     * @param textView  textview对象
     * @param size  文字大小
     */
    private void setTextSize(TextView textView,int size){
        textView.setTextSize(size);
    }
}