我的应用程序使用 unicode 字符\u2192 在 TextView 元素中显示右箭头。但是,箭头显示在最底线,但应垂直居中:
但是,如果我使用标准输出打印 unicode 字符,一切都很好:
public class Main {
public static void main(String[] args) {
System.out.println("A" + Character.toString("\u2192".toCharArray()[0]));
}
}
如何强制右箭头也位于 TextView 的中心?我的 TextView 已经使用了 android:gravity="center_vertical|left"
。
更新:我使用的是Android Studio 3.0.1和Android SDK 26。TextView的XML代码:
<TextView
android:id="@+id/my_text_view"
android:layout_width="match_parent"
android:fontFamily="monospace"
android:gravity="center_vertical|left"
android:textSize="26sp" />
在代码中填充TextView:
TextView textView = findViewById(R.id.my_text_view);
textView.setText("A" + Character.toString("\u2192".toCharArray()[0]) + "B");
最佳答案
自 Android's Roboto font doesn't seem to contain arrow glyphs ,这将导致它在可能的情况下显示后备字体中的字符(尽管我找不到有关此行为的文档)。我猜您设备上的“回退”箭头出于某种原因位于基线上。
但是,有一个技巧可能对您有所帮助:您可以包含特殊字符的图像并将该图像插入到输出文本中。
首先,在res/drawable
中添加一个新的Drawable资源文件(ic_arrow.xml
),然后复制粘贴到里面:
<vector android:alpha="0.78" android:height="24dp"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M3,12L21.5,12"
android:strokeColor="#000000" android:strokeWidth="1"/>
<path android:fillColor="#FF000000" android:pathData="M21,12L17,8"
android:strokeColor="#000000" android:strokeWidth="1"/>
<path android:fillColor="#FF000000" android:pathData="M21,12L17,16"
android:strokeColor="#000000" android:strokeWidth="1"/>
</vector>
现在,我们需要将箭头图像嵌入到可以显示在 TextView 中的 SpannableString 中。我将向您介绍完成此操作所需的(数量惊人的)代码行:
获取 Drawable 资源,以便我们可以在显示之前将其设置为正确的大小。
Drawable arrow = ContextCompat.getDrawable(this, R.drawable.ic_arrow);
弄清楚箭头需要多大,based on the font metrics .
Float ascent = textView.getPaint().getFontMetrics().ascent;
这是负数 ( for reasons ),因此将其设为正数。
int h = (int) -ascent;
最后,我们可以设置 Drawable 的边界来匹配文本 大小。
arrow.setBounds(0,0,h,h);
接下来,创建一个用我们的文本初始化的 SpannableString。我只是在这里使用
*
作为占位符,但它可以是任何字符(或空格)。如果您计划将不同的位与多个箭头图像连接在一起,请为此使用SpannableStringBuilder
。SpannableString stringWithImage = new SpannableString("A*B");
现在,我们终于可以将图像插入到 span 中(使用新的 ImageSpan 与我们的“箭头”图像,aligned with the baseline ).
1
和2
是从零开始和结束的索引(告诉在哪里 插入图像),SPAN_EXCLUSIVE_EXCLUSIVE
表示 the span doesn't expand to include inserted text .stringWithImage.setSpan(new ImageSpan(arrow, DynamicDrawableSpan.ALIGN_BASELINE), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
最后,我们可以显示文本(包括自定义箭头图像):
textView.setText(stringWithImage);
总而言之,代码应该如下所示。
TextView textView = findViewById(R.id.my_text_view);
Drawable arrow = ContextCompat.getDrawable(this, R.drawable.ic_arrow);
Float ascent = textView.getPaint().getFontMetrics().ascent;
int h = (int) -ascent;
arrow.setBounds(0,0,h,h);
SpannableString stringWithImage = new SpannableString("A*B");
stringWithImage.setSpan(new ImageSpan(arrow, DynamicDrawableSpan.ALIGN_BASELINE), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringWithImage);
我希望 Android 有更好的方法来做这类事情,但至少它可以以某种方式完成。生成的 TextView 应如下所示:
关于android - 为什么 TextView 在底行显示 unicode 右箭头 (\u2192)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49315570/