Android different Chinese character number alignment

Keywords: Android Java

Use spaces in the Android layout to align text. So how to represent a space in Android?

  • Blank space: &ා160;
  • Narrow space: &;

The space width of a Chinese character: &ා160; &ා160; &ා8210; [when two spaces (&ා160; &ා160;) occupy the width of a Chinese character, two spaces are slightly narrower than one Chinese character, and three spaces (&ා160; &ා160; &ා160;) are slightly wider than one Chinese character]

In practical use, it is necessary to use the combination of & (160; and & (8201); flexibly.

android:text = "real name:"

android:text = "body &" 160; "&" 160; copy & "160;" & "160; certificate:"

android:text = "last name & ා160; &ා160; &ා8210; first name:"

android:text = "ID card:" 

TextView implements the first line indent by:

  • In the string resource file, you can indent the first line by adding "\ u3000\u3000" in front of the text
  • In Java code, use setText("\u3000\u3000"+xxxxx);

Reproduced above from https://blog.csdn.net/u014651216/article/details/52411113

There is a problem in this way. When I am using & ා160; &ා160; &ා8210; the translation tiger of some models is not a space, but a "-".

Send another form &; half of Chinese characters are more accurate, &; a Chinese character will be a little wider than Chinese characters. Therefore, it is recommended to use &; for Chinese alignment, and & ෟ 160; &; has different performance in different models.

This form is only applicable to half character splicing. If it wasn't half, it wouldn't work. Character width can only be calculated by yourself

A common tool class, AlignedTextUtils, is used directly

SpannableStringBuilder retailName = AlignedTextUtils.justifyString("ID card", 4);
retailName.append(": ");
tvName.setText(retailName);
public class AlignedTextUtils {
    private static int n = 0;// Number of characters owned by the original Str
    private static SpannableString spannableString;
    private static double multiple = 0;// Magnification

    /**
     * Format the displayed string, such as input: date of birth output result: date of birth
     */
    public static String formatStr(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        str = ToSBC(str);
        n = str.length()-1;
        if (n >= 4) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        for (int i = n - 1; i > 0; i--) {
            sb.insert(i, "just");
        }
        return sb.toString();
    }

    /**
     * Format the display string, such as input: Android positive machine positive human output: Android robot
     *
     * @param str
     * @return     */
    public static SpannableString formatText(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        str = formatStr(str);
        if (str.length() <= 4) {
            return null;
        }
        spannableString = new SpannableString(str);
        switch (n) {

            case 3:
                multiple = 0.3333333333333333333333333333333333;
                break;

            default:
                break;
        }
        for (int i = 1; i < str.length()-1; i = i + 2)
        {
            spannableString.setSpan(new RelativeSizeSpan((float) multiple), i, i + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), i, i + 1,  Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }
    /**
     SBC case < br / > < br / >
     12288 for full space and 32 for half space
     The corresponding relationship between half angle (33-126) and full angle (65281-65374) of other characters is: the difference is 65248
     * @param input Any string
     * @return Half width string
     *
     */
    public static String ToSBC(String input)
    {
        //Half angle to full angle:
        char[] c=input.toCharArray();
        for (int i = 0; i < c.length; i++)
        {
            if (c[i]==32)
            {
                c[i]=(char)12288;
                continue;
            }
            if (c[i]<127)
                c[i]=(char)(c[i]+65248);
        }
        return new String(c);
    }
    public static SpannableStringBuilder justifyString(String str, int num) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        if (TextUtils.isEmpty(str)) {
            return spannableStringBuilder;
        }
        char[] chars = str.toCharArray();
        if (chars.length >= num || chars.length == 1) {
            return spannableStringBuilder.append(str);
        }
        int l = chars.length;
        float scale = (float) (num - l) / (l - 1);
        for (int i = 0; i < l; i++) {
            spannableStringBuilder.append(chars[i]);
            if (i != l - 1) {
                SpannableString s = new SpannableString(" ");//Full space
                s.setSpan(new ScaleXSpan(scale), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(s);
            }
        }
        return spannableStringBuilder;
    }
}

 

Posted by Huijari on Mon, 06 Jan 2020 04:20:26 -0800