lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package cn.sinata.xldutils.view;
 
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.sinata.xldutils.R;
import cn.sinata.xldutils.utils.DensityUtil;
 
/**
 *
 * Created by liaoxiang on 16/3/23.
 */
public class XTwoTextView extends LinearLayout{
 
    private TextView topView;
    private TextView bottomView;
    private ColorStateList topColor;
    private ColorStateList bottomColor;
 
    public XTwoTextView(Context context) {
        super(context);
        init(context,null);
    }
 
    public XTwoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }
 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public XTwoTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }
 
    void init(Context context,AttributeSet attrs){
        if (isInEditMode()) {
            return;
        }
        int padding = DensityUtil.dip2px(getContext(),16);
        setPadding(0,padding,0,padding);
        setOrientation(VERTICAL);
        topView = new TextView(getContext());
        topView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        topView.setSingleLine(true);
        topView.setGravity(Gravity.CENTER);
        bottomView = new TextView(getContext());
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.topMargin = DensityUtil.dip2px(getContext(),8);
        bottomView.setLayoutParams(params);
        bottomView.setSingleLine(true);
        bottomView.setGravity(Gravity.CENTER);
        CharSequence topText=null;
        CharSequence bottomText=null;
        int topTextSize = DensityUtil.sp2px(context,14);
        int bottomTextSize = DensityUtil.sp2px(context,14);
        if (attrs!=null){
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.XTwoTextView);
            topColor = a.getColorStateList(R.styleable.XTwoTextView_topTextColor);
            bottomColor = a.getColorStateList(R.styleable.XTwoTextView_bottomTextColor);
            topText = a.getText(R.styleable.XTwoTextView_topText);
            bottomText = a.getText(R.styleable.XTwoTextView_bottomText);
            topTextSize = a.getDimensionPixelSize(R.styleable.XTwoTextView_topTextSize,topTextSize);
            bottomTextSize = a.getDimensionPixelSize(R.styleable.XTwoTextView_bottomTextSize,bottomTextSize);
            a.recycle();
        }
 
        topView.setTextColor(topColor != null ? topColor : ColorStateList.valueOf(0xFF333333));
        bottomView.setTextColor(bottomColor != null ? bottomColor : ColorStateList.valueOf(0xFF333333));
 
        if (topText!=null) {
            topView.setText(topText);
        }
//        System.err.println("bottomText-->"+bottomText);
        if (bottomText != null) {
            bottomView.setText(bottomText);
        }
        setRawTextSize(topTextSize,bottomTextSize);
 
        addView(topView);
        addView(bottomView);
 
    }
 
    private void setRawTextSize(int topTextSize , int bottomTextSize){
        Context c = getContext();
        Resources r;
 
        if (c == null)
            r = Resources.getSystem();
        else
            r = c.getResources();
        topView.setTextSize( TypedValue.COMPLEX_UNIT_PX,TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_PX, topTextSize, r.getDisplayMetrics()));
        bottomView.setTextSize( TypedValue.COMPLEX_UNIT_PX,TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_PX, bottomTextSize, r.getDisplayMetrics()));
    }
 
    /**
     * 设置上边文字大小
     * @param size 默认单位sp
     */
    public void setTopTextSize(int size){
        topView.setTextSize(TypedValue.COMPLEX_UNIT_SP,size);
    }
    /**
     * 设置下边文字大小
     * @param size 默认单位sp
     */
    public void setBottomTextSize(int size){
        bottomView.setTextSize(TypedValue.COMPLEX_UNIT_SP,size);
    }
 
    /**
     * 设置上边文字
     * @param s 文字
     */
    public void setTopText(CharSequence s){
        topView.setText(s);
    }
    /**
     * 设置下边文字
     * @param s 文字
     */
    public void setBottomText(CharSequence s){
        bottomView.setText(s);
    }
 
    /**
     * 设置上边文字颜色
     * @param color 颜色
     */
    public void setTopTextColor(int color){
        topView.setTextColor(color);
    }
 
    /**
     * 设置下边文字颜色
     * @param color 颜色
     */
    public void setBottomTextColor(int color){
        bottomView.setTextColor(color);
    }
}