package com.jxw.zncd.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import com.jxw.zncd.R;
public class NoPaddingTextView extends AppCompatTextView {
public NoPaddingTextView(Context context) {
this(context, null);
}
public NoPaddingTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public NoPaddingTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.NoPaddingTextView, 0, 0);
int textSize = a.getDimensionPixelSize(R.styleable.NoPaddingTextView_textSize, (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getResources().getDisplayMetrics()));
Paint paint = new Paint();
Log.e("NoPaddingTextView", "textSize:" + textSize);
paint.setTextSize(textSize);
final Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
setTextSize(TypedValue.COMPLEX_UNIT_PX, a.getDimension(R.styleable.NoPaddingTextView_textSize, 12));
int top = (int) Math.ceil(Math.abs((fontMetricsInt.top - fontMetricsInt.ascent) / 2.0));
Log.e("NoPaddingTextView", "top" + top);
setPadding(0, -(Math.abs(fontMetricsInt.top - fontMetricsInt.ascent) + top)
, 0,
fontMetricsInt.top - fontMetricsInt.ascent);
a.recycle();
post(new Runnable() {
@Override
public void run() {
Log.e("NoPaddingTextView", "getHeight()" + getHeight());
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
attr属性
<declare-styleable name="NoPaddingTextView">
<attr name="textSize" format="dimension" />
</declare-styleable>
在xml中使用
<com.jxw.zncd.view.NoPaddingTextView
android:id="@+id/word_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/pinyin"
android:layout_centerInParent="true"
android:textColor="#949494"
android:textStyle="bold"
android:text="you"
android:includeFontPadding="false"
app:textSize="@dimen/zrpd_text_size"/>