Android 重力在我的自定义文本视图中不起作用
创建了自定义文本视图,因为我想给出字母之间的间距。它工作得很好。但我在重力方面有些问题。重力在我的自定义文本视图中不起作用。以下是我的自定义textview类代码:Android 重力在我的自定义文本视图中不起作用,android,textview,Android,Textview,创建了自定义文本视图,因为我想给出字母之间的间距。它工作得很好。但我在重力方面有些问题。重力在我的自定义文本视图中不起作用。以下是我的自定义textview类代码: public class MyTextView2D extends TextView { private Typeface typeface; private float letterSpacing = 0.0f; public MyTextView2D(Context context) {
public class MyTextView2D extends TextView {
private Typeface typeface;
private float letterSpacing = 0.0f;
public MyTextView2D(Context context) {
this(context, null);
isInEditMode();
}
public MyTextView2D(Context context, AttributeSet attrs) {
this(context, attrs, 0);
isInEditMode();
}
public MyTextView2D(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray attributesArray = getResources().obtainAttributes(attrs, R.styleable.MyTextView);
letterSpacing = attributesArray.getDimension(R.styleable.MyTextView_letterSpacing, 0.0f);
Log.d("letterSpacing", "letterSpacing "+ letterSpacing);
String fontName = attributesArray.getString(R.styleable.MyTextView_fontName);
if(!this.isInEditMode()) {
if (null == fontName) {
typeface = Fonts.getBlockBertholdRegular(context);
} else {
typeface = Fonts.get(context, fontName);
}
super.setTypeface(typeface);
}
this.invalidate();
}
public float getLetterSpacing() {
return letterSpacing;
}
public void setLetterSpacing(float letterSpacing) {
this.letterSpacing = letterSpacing;
}
@Override
protected void onDraw(Canvas canvas) {
Paint p = buildPaint();
CharSequence text = getText();
float x = getPaddingLeft();
float[] textWidths = new float[2];
p.setTypeface(getTypeface());
p.setTextSize(getTextSize());
Rect charBounds = new Rect();
for(int i = 0; i < text.length(); i++) {
String chr = "" + text.charAt(i);
p.getTextWidths(chr, textWidths);
canvas.drawText(chr, x, getTextSize(), p);
p.getTextBounds(chr, 0, 1, charBounds);
x += charBounds.right + getLetterSpacing();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Paint p = buildPaint();
CharSequence text = getText();
float x = getPaddingLeft();
float[] textWidths = new float[2];
p.setTypeface(getTypeface());
p.setTextSize(getTextSize());
Rect charBounds = new Rect();
for(int i = 0; i < text.length(); i++) {
String chr = "" + text.charAt(i);
p.getTextWidths(chr, textWidths);
p.getTextBounds(chr, 0, 1, charBounds);
x += charBounds.right + getLetterSpacing();
}
setMeasuredDimension((int)x, (int)getTextSize());
}
private Paint buildPaint() {
Paint p = new Paint();
p.setColor(getTextColors().getDefaultColor());
p.setStrokeWidth(1.5f);
p.setAntiAlias(true);
return p;
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
invalidate();
}
}
公共类MyTextView2D扩展了TextView{
私人字体;
专用浮动字母间距=0.0f;
公共MyTextView2D(上下文){
这个(上下文,空);
isInEditMode();
}
公共MyTextView2D(上下文、属性集属性){
这(上下文,属性,0);
isInEditMode();
}
公共MyTextView2D(上下文上下文、属性集属性、int defStyleAttr){
super(上下文、attrs、defStyleAttr);
TypedArray attributesArray=getResources().ActainAttributes(attrs,R.styleable.MyTextView);
letterSpacing=属性Array.getDimension(R.styleable.MyTextView\u letterSpacing,0.0f);
Log.d(“字母间距”、“字母间距”+字母间距);
String fontName=attributesArray.getString(R.styleable.MyTextView\u fontName);
如果(!this.isInEditMode()){
if(null==fontName){
字体=字体。getBlockBertholdRegular(上下文);
}否则{
typeface=Fonts.get(上下文,fontName);
}
super.setTypeface(字体);
}
这个。使无效();
}
公共浮点数getLetterSpacing(){
返回字母间距;
}
公共空白设置字体间距(浮动字体间距){
此参数。字母间距=字母间距;
}
@凌驾
受保护的void onDraw(画布){
Paint p=buildPaint();
CharSequence text=getText();
float x=getPaddingLeft();
float[]文本宽度=新的float[2];
p、 setTypeface(getTypeface());
p、 setTextSize(getTextSize());
Rect charBounds=new Rect();
对于(int i=0;i
在重力作用下我做什么?也许另一个属性也不起作用,但我知道它不属于我们
编辑
你能将你的xml文件发布到你指定重力的地方吗?我可以,我想知道在哪里以及如何编写添加super.onMeasure(WidthMeasure,HeightMeasure)在您的onMeasure()
方法中。
<mypakeg.customviews.MyTextView2D
android:id="@+id/subtotal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="15px"
android:text="$0.00"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#333333"
android:textSize="11.75sp"
android:textStyle="bold"
skip:letterSpacing="1px"
android:layout_weight="0.80"/>