【问题标题】:Android two Textviews, align them as one paragraphAndroid 两个Textview,对齐为一个段落
【发布时间】:2017-07-05 23:27:33
【问题描述】:

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

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

希望:

我目前所拥有的:

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

谢谢!

【问题讨论】:

标签: android user-interface textview alignment android-relativelayout


【解决方案1】:

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

【讨论】:

  • @who 否决了这个?请问原因?如果有问题我可以修复它们
【解决方案2】:

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

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

也可以查看answer了解详情

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 2014-01-26
      相关资源
      最近更新 更多