为什么 TextView 在底行显示 unicode 右箭头 (\u2192)?
Why is TextView showing the unicode right arrow (\u2192) at the bottom line?
我的应用程序使用 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");
尝试使用 html 实体编码的其他符号。看看这个link,也许你觉得这里更方便向右箭头但是我选择了这个例如:➔
String formula = "A ➔ B";
textView.setText(Html.fromHtml(formula));
还有这个第三方lib可能适合你
试试看:
SpannableString st = new SpannableString("A" + "\u2192" + "B");
st.setSpan(new RelativeSizeSpan(2f), 1, 2, 0);
textView.setText(st);
我知道这并不能完全回答。
自 Android's Roboto font doesn't seem to contain arrow glyphs 以来,这将导致它在可能的情况下显示后备字体中的字符(尽管我找不到有关此行为的文档)。我猜你设备上的 "fallback" 箭头出于某种原因位于基线上。
但是,有一个技巧可能对您有所帮助:您可以包含特殊字符的图像并将该图像插入到输出文本中。
首先,在res/drawable
中添加一个新的Drawable资源文件(ic_arrow.xml
),并将copy-paste这个放入其中:
<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);
确定箭头的大小,根据字体
指标.
Float ascent = textView.getPaint().getFontMetrics().ascent;
这是负数 (for reasons),因此将其设为正数。
int h = (int) -ascent;
最后我们可以设置Drawable的bounds来匹配文字
尺寸。
arrow.setBounds(0,0,h,h);
接下来,创建一个用我们的文本初始化的 SpannableString。我只是在这里使用 *
作为占位符,但它可以是任何字符(或空白 space)。如果您打算将不同的位与多个箭头图像连接在一起,请为此使用 SpannableStringBuilder
。
SpannableString stringWithImage = new SpannableString("A*B");
现在,我们终于可以将图像插入到 span 中(使用新的
ImageSpan 与我们的 "arrow" 图像,aligned with the baseline)。
1
和 2
是 zero-based 开始和结束索引(告诉在哪里
插入图片),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 应如下所示:
我的应用程序使用 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");
尝试使用 html 实体编码的其他符号。看看这个link,也许你觉得这里更方便向右箭头但是我选择了这个例如:➔
String formula = "A ➔ B";
textView.setText(Html.fromHtml(formula));
还有这个第三方lib可能适合你
试试看:
SpannableString st = new SpannableString("A" + "\u2192" + "B");
st.setSpan(new RelativeSizeSpan(2f), 1, 2, 0);
textView.setText(st);
我知道这并不能完全回答。
自 Android's Roboto font doesn't seem to contain arrow glyphs 以来,这将导致它在可能的情况下显示后备字体中的字符(尽管我找不到有关此行为的文档)。我猜你设备上的 "fallback" 箭头出于某种原因位于基线上。
但是,有一个技巧可能对您有所帮助:您可以包含特殊字符的图像并将该图像插入到输出文本中。
首先,在res/drawable
中添加一个新的Drawable资源文件(ic_arrow.xml
),并将copy-paste这个放入其中:
<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);
确定箭头的大小,根据字体 指标.
Float ascent = textView.getPaint().getFontMetrics().ascent;
这是负数 (for reasons),因此将其设为正数。
int h = (int) -ascent;
最后我们可以设置Drawable的bounds来匹配文字 尺寸。
arrow.setBounds(0,0,h,h);
接下来,创建一个用我们的文本初始化的 SpannableString。我只是在这里使用
*
作为占位符,但它可以是任何字符(或空白 space)。如果您打算将不同的位与多个箭头图像连接在一起,请为此使用SpannableStringBuilder
。SpannableString stringWithImage = new SpannableString("A*B");
现在,我们终于可以将图像插入到 span 中(使用新的 ImageSpan 与我们的 "arrow" 图像,aligned with the baseline)。
1
和2
是 zero-based 开始和结束索引(告诉在哪里 插入图片),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 应如下所示: