android - 如何从 TextAppearance 获取字母间距
问题描述
我们正在使用一个包含所有样式的库,例如字母间距。这在我们的自定义视图中造成了一些问题,因为我们需要正确使用 TextAppearance。
我通过 XML 传递 TextAppearance 并尝试在油漆上使用它的属性。这适用于其他属性,例如文本大小和 FontTypeFace,但由于缺少textAppearanceSpan.getLetterSpacing()
函数而不起作用。
XML 是通过声明的 styleable 传递的
attrs.xml
<declare-styleable name="MyCustomView">
<attr name="TextAppearance" />
</declare-styleable>
布局.xml
<MyCustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:TextAppearance="?attr/TextAppearanceBig" />
然后我阅读了这些元素
MyCustomView.java
Paint mPaint;
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, 0, 0);
int textAppearanceID = typedArray.getResourceId(R.styleable.MyCustomView_TextApperance);
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(context, textApperanceID);
mPaint = new Paint();
mPaint.setTextSize(textAppearanceSpan.getTextSize());
mPaint.setTypeface(Typeface.create(textAppearanceSpan.getFamily(), Typeface.NORMAL);
}
@Override
protected synchronized void onDraw(Canvas canvas){
canvas.drawText("Hello World", 0, 0, mPaint);
}
我想做,mPaint.setLetterSpacing(textAppearanceSpan.getLetterSpacing())
但该功能textAppearanceSpan.getLetterSpacing()
不存在。
解决方案
getLetterSpacing()是 Paint 类的方法,因此您可以将它与paint
对象一起使用。
它的默认值为 0
setLetterSpacing (float letterSpacing)是TextView
谢谢你。
推荐阅读
- python - 使用 python 创建条形图,每周显示数据
- javascript - 无法使用 ReactJS 在输入字段中输入文本值
- html - 如何在 Shiny 中默认选择 verbatimTextOutput 中的文本?
- laravel - paytm pg 错误代码 419(页面已过期),即使表单数据在交易中成功
- r - R one-liner 用于反转列表的嵌套顺序?
- google-sheets - 如何根据 Google 表格中相邻列中的值返回一系列单元格
- java - 按下按钮时没有任何反应
- java - 使用 Jackson 库时如何实现错误处理程序
- reactjs - 如何使用 Jest 使用新的 React Router Hooks 模拟 history.push
- r - 如果列标题位于两个日期之间,则返回值