如何使用特定TextAppearance
绘制文字?
这是我的代码。
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setFakeBoldText(false);
paint.setTextSize(textSize);
// TODO: set the style of the text to a specific TextAppearance. E.g. @android:style/TextAppearance.DeviceDefault.Large.Inverse
canvas.drawText(context.getResources().getString(R.string.myString), textOffsetX, textOffsetY, paint);
我查看了TypeFace
对象,但我不确定如何从TextAppearance创建TypeFace。
答案 0 :(得分:2)
同事给了我一个解决方案,使用TextView
作为TextAppearance
TextView textView = new TextView(context);
textView.setTextAppearance(context, android.R.style.TextAppearance.DeviceDefault.Large);
paint.setColor(textView.getCurrentTextColor());
paint.setTextSize(textView.getTextSize());
paint.setTypeface(textView.getTypeface());
答案 1 :(得分:0)
基于louis.luo的响应,我找到了适用于所有API版本的解决方案。您必须使用AppCompatTextView
和TextViewCompat.setTextAppearance
:
TextView textView = new AppCompatTextView(getContext());
TextViewCompat.setTextAppearance(textView, styleRes);
//Just get paint from textview like janosch suggests
paint = textView.getPaint();
//Or get some info from textview
paint.setColor(textView.getCurrentTextColor());
paint.setTextSize(textView.getTextSize());
paint.setTypeface(textView.getTypeface());