Android自定义文本视图[关闭]

问题描述 投票:0回答:2

我需要文本视图,其中第一个字母大写,如照片所示。实现这一目标的最佳库或解决方案是什么?

enter image description here

android textview android-custom-view
2个回答
1
投票

1. 在 app/build.gradle 中添加以下依赖

compile 'com.github.rpradal.lettrine:lettrine:release_number'

2. 使用 LettrineTextView

<com.github.rpradal.lettrine.LettrineTextView
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              app:lettrine_textColor="@android:color/holo_red_dark"
              app:lettrine_text="Lorem ipsum"
              app:lettrine_lettrineSize="3"
              app:lettrine_textSize="14sp" />

如需更多帮助,请点击链接

结果如下:

enter image description here


1
投票

您无需使用任何库即可使用

SpannableString

  String title = "This is a very good thing. You should try with that and suggest to others";
  final SpannableString spannableString = new SpannableString(title);
  int position = 0;
  for (int i = 0, ei = title.length(); i < ei; i++) {
        char c = title.charAt(i);
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
            position = i;
            break;
        }
    }

  spannableString.setSpan(new RelativeSizeSpan(2.0f), position, position + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  txt.setText(spannableString, TextView.BufferType.SPANNABLE);

您可以在

title
变量中使用文本。

© www.soinside.com 2019 - 2024. All rights reserved.