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

时间:2017-02-16 17:32:38

标签: android user-interface textview alignment relativelayout

假设我有两个textviews,一个标题和详细信息,详细信息是 toEndOf 标题(同一行),而标题是一行,详细信息,可以是多个,我的问题是,如何配置详细信息textview,当它开始一个新行时,而不是将其与textview的上一行对齐,将其与标题{{1}对齐因此创建一个段落感觉。

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

所需enter image description here

到目前为止我有什么:

enter image description here

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

谢谢!

3 个答案:

答案 0 :(得分:0)

是的,正如您所提到的,我还看到一个TextView内部文本的字符串大小/颜色不同。

要实现此目的,您需要使用SpannableString - Docs link

另请查看此answer了解详情

答案 1 :(得分:0)

使用SpannableText解决您的问题以分隔两个文字视图。

答案 2 :(得分:0)

您可以使用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);
}

enter image description here