wjc
4 小时以前 d22974af03fb5a008cf1187e7b96cb37ea0b3093
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
package com.hdl.photovoltaic.widget;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
 
import androidx.appcompat.widget.AppCompatTextView;
 
import com.hdl.photovoltaic.R;
 
 
public class AutoAlignmentTextView extends AppCompatTextView {
 
    private int singleLineAlignment = Gravity.END;
    private int multiLineAlignment = Gravity.START;
 
    public AutoAlignmentTextView(Context context) {
        super(context);
        init(null);
    }
 
    public AutoAlignmentTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }
 
    public AutoAlignmentTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }
 
    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(
                    attrs, R.styleable.AutoAlignmentTextView);
 
            int singleLine = a.getInt(R.styleable.AutoAlignmentTextView_singleLineAlignment, 2);
            int multiLine = a.getInt(R.styleable.AutoAlignmentTextView_multiLineAlignment, 0);
 
            singleLineAlignment = getGravityFromAttr(singleLine);
            multiLineAlignment = getGravityFromAttr(multiLine);
 
            a.recycle();
        }
 
        // 默认右对齐
        setGravity(singleLineAlignment);
 
        // 监听文本变化
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
 
            @Override
            public void afterTextChanged(Editable s) {
                checkAndUpdateAlignment();
            }
        });
    }
 
    private int getGravityFromAttr(int value) {
        switch (value) {
            case 0: return Gravity.START;
            case 1: return Gravity.CENTER_HORIZONTAL;
            case 2: return Gravity.END;
            default: return Gravity.END;
        }
    }
 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        checkAndUpdateAlignment();
    }
 
    private void checkAndUpdateAlignment() {
        // 使用post确保在布局完成后执行
        post(() -> {
            boolean isWrapped = checkIfTextWrapped();
            if (isWrapped) {
                setGravity(multiLineAlignment);
            } else {
                setGravity(singleLineAlignment);
            }
        });
    }
 
    private boolean checkIfTextWrapped() {
        // 方法1: 检查布局行数(最准确)
        if (getLayout() != null) {
            return getLayout().getLineCount() > 1;
        }
 
        // 方法2: 计算文本宽度
        Paint paint = getPaint();
        String text = getText().toString();
 
        if (TextUtils.isEmpty(text)) {
            return false;
        }
 
        float textWidth = paint.measureText(text);
        int availableWidth = getWidth() - getPaddingLeft() - getPaddingRight();
 
        // 考虑省略号的情况
        if (getEllipsize() != null) {
            // 如果有省略号,说明文本可能被截断
            return textWidth >= availableWidth;
        }
 
        return textWidth > availableWidth;
    }
 
    /**
     * 手动触发对齐检查
     */
    public void refreshAlignment() {
        checkAndUpdateAlignment();
    }
}