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();
|
}
|
}
|