android - Android 的零宽度换行空间

标签 android

有谁知道\u200b 是否应该在 Android 上作为零宽度空间工作,如果 TextView 的文本超过 TextView 的长度,则该空间用作换行符?看来只有\u0020 对我来说是换行符,但我无法弄清楚如何拥有它的零宽度版本。\u200b 是我期望的工作,根据以下链接,但它只做零宽度空间并且不会中断......并且如前所述,只有\u0020 是换行符。

http://www.cs.tut.fi/~jkorpela/chars/spaces.html

我附上了我用来测试 U+ 代替\u 的 Activity 的 View 。

我也尝试使用 fromHtml 选项来查看是否有一个 Html 选项有效,但没有任何运气使用 arial。

这是我正在使用的测试代码

public class TextSpaceActivity extends Activity {

public static void start( Context ctx ) {
    ctx.startActivity(  new Intent( ctx, TextSpaceActivity.class )  );
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.text_space_activity );
    setTitle( "TextSpaceActivity" );

    setText( R.id.tsa_txvw_1, "abc\u0020123\u0020xyz\u0020987" );
    setText( R.id.tsa_txvw_2, "abc\u200a123\u200axyz\u200a987" );
    setText( R.id.tsa_txvw_3, "abc\u200b123\u200bxyz\u200b987" );
}

TextView txvw;
private void setText( int txvwResId, String txt ) {
    txvw = (TextView)findViewById( txvwResId );
    txvw.setText( txt );
}
}

enter image description here

最佳答案

我不相信换行算法理解零宽度换行符、软连字符或行或段落分隔符字符。这是来自 Android 源代码的代码,它决定此处是否可以换行(android.text.StaticLayout,源代码中的第 358-366 行):

// From the Unicode Line Breaking Algorithm (at least approximately)
boolean isLineBreak = isSpaceOrTab ||
        // / is class SY and - is class HY, except when followed by a digit
        ((c == CHAR_SLASH || c == CHAR_HYPHEN) &&
        (j + 1 >= spanEnd || !Character.isDigit(chs[j + 1 - paraStart]))) ||
        // Ideographs are class ID: breakpoints when adjacent, except for NS
        // (non-starters), which can be broken after but not before
        (c >= CHAR_FIRST_CJK && isIdeographic(c, true) &&
        j + 1 < spanEnd && isIdeographic(chs[j + 1 - paraStart], false));

其中 isSpaceOrTab 在上面(第 343 行)定义为:

boolean isSpaceOrTab = c == CHAR_SPACE || c == CHAR_TAB;

所有 CHAR_ 常量都是纯字符常量,所以没有像 isspace 这样的东西。同一文件中的第 952-958 行:

private static final char CHAR_FIRST_CJK = '\u2E80';

private static final char CHAR_NEW_LINE = '\n';
private static final char CHAR_TAB = '\t';
private static final char CHAR_SPACE = ' ';
private static final char CHAR_SLASH = '/';
private static final char CHAR_HYPHEN = '-';

查看您的其他评论,我看到您正在尝试正确破解中文。您可能不需要做任何特别的事情:正如上面的 isIdeographic 调用所暗示的那样,它会尝试在两个表意文字之间中断而不插入空格。只有 StaticLayout 中断器会这样做:DynamicLayout 只使用换行符,因此它只会在静态文本上正确中断。

恐怕根据我的研究,您看起来像是被搞砸了。我对解决方法的唯一建议是使用 WebView 而不是 TextView,并使用系统 Web 浏览器的卓越换行功能而不是有限的实现 TextView 提供。

关于android - Android 的零宽度换行空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13594979/

相关文章:

三星 Galaxy SIII 上的 Android OpenGL 透明度

android - 自定义paypal android sdk源码

android - 解析 API 推送通知,在 Play 商店打开应用页面

java - Volley NetworkImageView - 如何回收位图?

android - 默认情况下,应用程序链接不会在 Android 12 中打开。可能的 SHA256 问题

android - 带 fragment 的按钮

android - 安装android默认示例代码

android - 找不到符号 Volley

java - 合并两个 ContentView

android - 蓝牙丢包怎么恢复??安卓