Why does the TextView show the unicode right arrow (\ u2192) in the bottom line?

My application uses the unicode \ u2192 character to display the right arrow in a TextView element. However, the arrow is shown on the bottom line, but should be centered vertically:

enter image description here

However, if I print the Unicode character using standard output, everything is fine:

public class Main {
  public static void main(String[] args) {
    System.out.println("A" + Character.toString("\u2192".toCharArray()[0]));
  }
}

enter image description here

How can I provide the right arrow to center in a TextView? My TextView is already using android:gravity="center_vertical|left".

Update . I am using Android Studio 3.0.1 and 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" />

Filling a TextView in code:

    TextView textView = findViewById(R.id.my_text_view);
    textView.setText("A" + Character.toString("\u2192".toCharArray()[0]) + "B");
+4
source share
3 answers

Android Roboto, , , , ( ). , "" - .

, : .

Drawable (ic_arrow.xml) res/drawable :

<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>

SpannableString, TextView. ( ) , :

  • Drawable, , .

    Drawable arrow = ContextCompat.getDrawable(this, R.drawable.ic_arrow);

  • , .

    Float ascent = textView.getPaint().getFontMetrics().ascent;

  • ( ), .

    int h = (int) -ascent;

  • , Drawable .

    arrow.setBounds(0,0,h,h);

  • SpannableString, . * , ( ). , SpannableStringBuilder.

    SpannableString stringWithImage = new SpannableString("A*B");

  • , , ( ImageSpan "", ). 1 2 ( ), SPAN_EXCLUSIVE_EXCLUSIVE , span , .

    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 :

screenshot

+2

:

SpannableString st = new SpannableString("A" + "\u2192" + "B");
st.setSpan(new RelativeSizeSpan(2f), 1, 2, 0);
textView.setText(st);

, , , .

+1

Try using other characters encoded with html entities. Look at the link, you may find a more convenient right arrow here for you, but I chose this, for example:

String formula = "A &#10132 B";
textView.setText(Html.fromHtml(formula));

also this third party lib is probably right for you

0
source

Source: https://habr.com/ru/post/1694957/


All Articles