Android 사용자 정의 TextView padding Top 과 padding Bottom 제거
최근 프로젝트 에 서 는 libgdx 로 안 드 로 이 드 TextView 를 렌 더 링 해 야 합 니 다.그러나 그 려 진 TextView 는 기본적으로 paddingTop 과 paddingBottom 을 가지 고 있 습 니 다.다음 그림 과 같 습 니 다.
인터넷 에 많은 해결 방안 이 있 습 니 다.예 를 들 어 xml 에 다음 과 같은 속성 을 설정 합 니 다.
android:lineSpacingMultiplier="0.8"
android:includeFontPadding="false"
margin 을 마이너스 로 설정 하 는 등.하지만 이 방법 은 6.0 이후 에는 별 쓸모 가 없다.사용자 정의 TextView
package com.ef.smallstar.common.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
/**
* Created by Danny on 17/8/28.
*
* this is a Android TextView without padding top & padding bottom
*/
public class TextViewWithoutPadding extends android.support.v7.widget.AppCompatTextView {
private final Paint mPaint = new Paint();
private final Rect mBounds = new Rect();
public TextViewWithoutPadding(Context context) {
super(context);
}
public TextViewWithoutPadding(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewWithoutPadding(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(@NonNull Canvas canvas) {
final String text = calculateTextParams();
final int left = mBounds.left;
final int bottom = mBounds.bottom;
mBounds.offset(-mBounds.left, -mBounds.top);
mPaint.setAntiAlias(true);
mPaint.setColor(getCurrentTextColor());
canvas.drawText(text, -left, mBounds.bottom - bottom, mPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
calculateTextParams();
setMeasuredDimension(mBounds.width() + 1, -mBounds.top + 1);
}
private String calculateTextParams() {
final String text = getText().toString();
final int textLength = text.length();
mPaint.setTextSize(getTextSize());
mPaint.getTextBounds(text, 0, textLength, mBounds);
if (textLength == 0) {
mBounds.right = mBounds.left;
}
return text;
}
}
궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나 본 사이트 의 커 뮤 니 티 에 가서 토론 을 교류 하 세 요.읽 어 주 셔 서 감사합니다. 도움 이 되 셨 으 면 좋 겠 습 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.