在同一TextView中具有不同语言的SpannableString

时间:2014-08-20 07:49:41

标签: android spannablestring spannable

假设我在ListView中有一个TextView。

TextView的名称是姓名(阿拉伯语)+“共享”+姓名(英文)。

你知道的英语从左到右开始但阿拉伯语从右到左开始

当两个名字都用英文写成时,其工作正常

但是当第一个名字是阿拉伯语时,它将是这样的

“共享”+姓名(英文)+姓名(阿拉伯语)

我尝试过AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE)

但它不起作用

这是我的代码:

    SpannableString ss = new SpannableString(PostUserName_ + " " + share + " "
                + uname.trim());

        ClickableSpan FirstName = new ClickableSpan() {
            @Override
            public void onClick(View v) {

                if (listener != null) {
                    listener.onFirstName(v, position, 3);
                }
            }

        };

        ClickableSpan LastName = new ClickableSpan() {
            @Override
            public void onClick(View v) {

                if (listener != null) {
                    listener.onLastName(v, position, 3);
                }
            }

        };

        ss.setSpan((new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE)),0, PostUserName_.length() + share.length() + 1+uname.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new TextAppearanceSpan(context,
                android.R.style.TextAppearance_Small), PostUserName_.length(),
                PostUserName_.length()+share.length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        ss.setSpan(FirstName, 0, PostUserName_.length() ,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(LastName, PostUserName_.length() + share.length() + 2,
                PostUserName_.length() + share.length() + 1+uname.length()  ,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new RelativeSizeSpan(0.8f), PostUserName_.length() + 1,
                 PostUserName_.length()+share.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        ss.setSpan(new ForegroundColorSpan(Color.BLACK), 0, PostUserName_.length() + share.length() + 1+uname.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.PostUserName.setVisibility(View.VISIBLE);
        holder.PostUserName.setText(ss);

1 个答案:

答案 0 :(得分:0)

如果可能的话,使用两个TextViews用于英语和阿拉伯语并将它们左右对齐..