Android:多个textview,如单个文本

时间:2013-10-23 11:32:40

标签: android textview spannablestring

需要像一个独特的文本块一样接着格式化几个textview,有没有办法实现这个目标?

像这样

__________________________
|                        |
|                        |
| aaaaaa bbbbbbbbbbbbbbb |
| bbbbbbbbbb cccccc dddd |
| ddddddddddddd.         |
|                        |

我无法将textview放在LinearLayout中,因为我将获得此效果:

__________________________
|                        |
|                        |
| aaaaaa bbbbbbbbbbbbbbb |
|        bbbbbbbbbb      |
|                        |
|                        |

任何想法?

更新:解决了SPANNABLES

    String[] testString = { "this is a test ", "for testing click in different region", "of the textview"};
    LinearLayout linear = new LinearLayout(this);
    TextView textview = new TextView(this);
    textview.setMovementMethod(LinkMovementMethod.getInstance());
    SpannableStringBuilder builder = new SpannableStringBuilder();
    int start = 0;
    int end = 0;

    for (String span : testString) {
        start = end == 0 ? end : end + 1;
        end = start + span.length();
        final String spanned = span;
        builder.append(span);
        builder.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Log.e("Clicked on:", spanned);
            }
        }, start, end, 0);
        Log.e("TEST", span);
    }

    textview.append(builder);

    linear.addView(textview);
    setContentView(linear);

1 个答案:

答案 0 :(得分:2)

如果你确实需要,可以使用FlowLayout。但是,如果可以,您应该使用spannables使用一个textView和格式文本。