在TextView之后继续新行TextView

时间:2016-12-09 14:51:47

标签: java android

我有一个用例,当只需要一部分文字可点击且颜色不同时,在某些屏幕上我的文字必须用2行写成:enter image description here

但我明白了: enter image description here

使用此代码:

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/btn_accept_terms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/selected" />

        <TextView
            android:id="@+id/terms1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/btn_accept_terms"
            android:text="@string/txt_terms" />

        <TextView
            android:id="@+id/terms2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/terms1"
            android:text="@string/txt_terms_hyperlink"
            android:textColor="@color/colorAccent"
            android:textStyle="italic"/>
    </RelativeLayout>

我如何实现预期的行为?

2 个答案:

答案 0 :(得分:0)

使用spannable

public static SpannableStringBuilder addClickablePart(final Context context, String str) {
    int[] startIndex = {32, 73};
    int[] endIndex = {45, str.length()};
    SpannableStringBuilder ssb = new SpannableStringBuilder(str);
    int count = 0;
    while (count < 2) {
        int idx1 = startIndex[count];
        int idx2 = endIndex[count];
        final String clickString = str.substring(idx1, idx2);
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                // Do whatever you want to do
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
                ds.setColor(Color.parseColor("#ffffffff"));
            }
        }, idx1, idx2, 0);
        count++;
    }
    return ssb;
}

相应地调整startIndex和endIndex

答案 1 :(得分:0)

使用SpannableString。请参阅下面的代码。

String part1 = "By logging in, you agree to our ";
String part2 = "terms and conditions";
String fullStr = part1 + part2;
int startIndex = fullStr.indexOf(part2);
int endIndex = fullStr.length();

SpannableString styledString = new SpannableString(fullStr);

// clickable text
ClickableSpan clickableSpan = new ClickableSpan() {

    @Override
    public void onClick(View widget) {
        Toast.makeText(TestActivity.this, "Navigate to terms and conditions page", Toast.LENGTH_SHORT).show();
    }
};
styledString.setSpan(clickableSpan, startIndex, endIndex, 0);

// set color
styledString.setSpan(new ForegroundColorSpan(Color.BLUE), startIndex, endIndex, 0);

TextView out = (TextView) findViewById(R.id.out);
out.setMovementMethod(LinkMovementMethod.getInstance());
out.setText(styledString);

要从ClickableSpan中删除下划线,请参阅this SO thread