Android 两个Textview,将它们对齐为一个段落

Android two Textviews, align them as one paragraph

假设我有两个 textviews,一个标题和详细信息,详细信息是 toEndOf 标题(同一行),而标题是一行, 细节, 可以是多个, 我的问题是, 我如何配置细节 textview 当它开始一个新行时, 而不是将它对齐到 textview 的前一行, 将它对齐到标题 textview,从而营造出段落感。

请查看这些屏幕截图以了解我要完成的工作(标题是粗体文本,其余部分详细说明):

需要

我目前有:

我能想到的唯一解决方案是对两者使用一个 textview,并使用 HTML 格式化文本,但是如果我需要更大的标题文本大小怎么办?

谢谢!

是的,正如你提到的,我还看到一个 TextView 里面的文本有不同的字符串 sizes/colors。

要实现这一点,您需要使用 SpannableString - Docs link

另请检查此 answer 了解详细信息

针对您的问题使用 SpannableText 来分隔两个文本视图。

您可以使用SpannableText

这就够了!

 <TextView
    ..
    android:id="@+id/text_view"
    />

...

TextView text=(TextView)findViewById(R.id.text_view);
String head = "Seat (s):";
String body = "  The  baby name  means  the meaning of the nam";
setTextWithSpan(text,head+body,head, body,new android.text.style.StyleSpan(android.graphics.Typeface.BOLD));

自定义方法

public  void setTextWithSpan(TextView textView, String text, String spanTextBold,String secondPartOfText,StyleSpan style) {

    SpannableStringBuilder sb = new SpannableStringBuilder(text);

    int start = text.indexOf(spanTextBold);
    int end = start + spanTextBold.length();
    sb.setSpan(style, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
    //sb.setSpan(new ForegroundColorSpan(Color.BLUE), start, end ,0); // if you need a color

    int startTwo = text.indexOf(secondPartOfText);
    int endTwo = startTwo + secondPartOfText.length();
    // sb.setSpan(new StyleSpan(Typeface.ITALIC),startTwo,endTwo , 0);
    sb.setSpan(new RelativeSizeSpan(0.8f), startTwo, endTwo, 0);

    textView.setText(sb);
}