Android 使用数据库中的项目符号设置多行文字格式
我有一个文本视图,其中显示数据库中的文本,其中可以包括要点: 我的文本视图位于带有线性布局的滚动视图中,没有任何特殊内容:Android 使用数据库中的项目符号设置多行文字格式,android,android-layout,textview,android-database,Android,Android Layout,Textview,Android Database,我有一个文本视图,其中显示数据库中的文本,其中可以包括要点: 我的文本视图位于带有线性布局的滚动视图中,没有任何特殊内容: <TextView android:id="@+id/textview_quiz_infotext" android:layout_width="wrap_content" android:layout_height="wrap_c
<TextView
android:id="@+id/textview_quiz_infotext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
tools:text="- This is the text" />
当文本超过一行时,我想添加与第一行相同的间距。
看起来是这样的:
有可能实现这种行为吗?我也在尝试这样做。我用一个RecyclerView和一个适配器进行了尝试,然后找到了这篇文章。
我在我的旧项目中成功地解决了这个问题。我们的想法是首先对文本进行一些预处理,以便在文本中只包含项目符号分隔符,假设根据上面的文本,项目符号分隔符是“-”。然后提取每个项目符号线并使用项目符号span()进行渲染,或者如果您需要更多自定义来渲染您自己的项目符号,您可以使用引线边距span(),它为您提供了使用引线边距方法绘制自定义项目符号点的灵活性。在我的例子中,我创建了以下Bullet Util类:
public class BulletUtil {
public enum BulletStyle{
Style1("•", "\u2022"),
Style2("●", "\u25CF"),
Style3("○", "\u25CB"),
Style4("▪", "\u25AA"),
Style5("■", "\u25A0"),
Style6("□", "\u25A1"),
Style7("►", "\u25BA");
private String symbol;
private String code;
private BulletStyle(String symbol, String code){
this.symbol = symbol;
this.code = code;
}
public String getCode() {
return code;
}
}
/**
* Creates Leading Margin Spans {@link LeadingMarginSpan} based on a gap width and a color integer.*
* @param originText the original Text
* @param bulletDelimiter the bullet delimiter
* @param bulletGapWidth the distance, in pixels, between the bullet point and the paragraph.
* @param bulletColor the bullet point color, as a color integer.
* @param bulletStyle the bullet style
* @return CharSequence with BulletSpans
*/
public static CharSequence addLeadingBullets(String originText, String bulletDelimiter, int bulletGapWidth, @ColorInt int bulletColor, BulletStyle bulletStyle) {
return createLeadingMarginSpans(bulletGapWidth, bulletColor, getBulletList(originText, bulletDelimiter), bulletStyle);
}
/**
* Creates Bullet Spans {@link BulletSpan} based on a gap width and a color integer.*
* @param originText the original Text
* @param bulletDelimiter the bullet delimiter
* @param bulletGapWidth the distance, in pixels, between the bullet point and the paragraph.
* @param bulletColor the bullet point color, as a color integer.
* @return CharSequence with BulletSpans
*/
public static CharSequence addBullets(String originText, String bulletDelimiter, int bulletGapWidth, @ColorInt int bulletColor) {
return createBulletSpans(bulletGapWidth, bulletColor, getBulletList(originText, bulletDelimiter));
}
/**
* Retrieves Bullet List based on a Bullet Delimiter
* @param text the Text
* @param bulletDelimiter the bullet delimiter
* @return List<String> the bullet List
*/
public static List<String> getBulletList(String text, String bulletDelimiter){
List<String> bulletLines = Arrays.asList(text.split(bulletDelimiter));
List<String> bulletList = new ArrayList<>();
if(bulletLines!=null && bulletLines.size()>0){
for(int i=0; i<bulletLines.size(); i++){
String bulletLine = bulletLines.get(i);
if(!TextUtils.isEmpty(bulletLine)){
bulletList.add(bulletLine);
}
}
}
return bulletList;
}
/**
* Creates Bullet Spans {@link BulletSpan} based on a gap width and a color integer.
*
* @param bulletGapWidth the distance, in pixels, between the bullet point and the paragraph.
* @param bulletColor the bullet point color, as a color integer.
* @param bulletList the bullet List lines
*/
public static CharSequence createBulletSpans(int bulletGapWidth, @ColorInt int bulletColor, @NonNull List<String> bulletList) {
SpannableStringBuilder sbb = new SpannableStringBuilder();
for(int i=0; i<bulletList.size(); i++){
CharSequence bulletLine = bulletList.get(i) + (i < bulletList.size()-1 ? "\n" : "");
Spannable spannable = new SpannableString(bulletLine);
BulletSpan bulletSpan = new BulletSpan(bulletGapWidth, bulletColor);
spannable.setSpan(bulletSpan, 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
sbb.append(spannable);
}
return sbb;
}
/**
* Creates Bullet LeadingMarginSpans {@link LeadingMarginSpan} based on a gap width and a color integer.
*
* @param bulletGapWidth the distance, in pixels, between the bullet point and the paragraph.
* @param bulletColor the bullet point color, as a color integer.
* @param bulletList the bullet List lines
* @param bulletStyle the bullet enum Style
*/
public static CharSequence createLeadingMarginSpans(int bulletGapWidth, @ColorInt int bulletColor, @NonNull List<String> bulletList, BulletStyle bulletStyle) {
SpannableStringBuilder sbb = new SpannableStringBuilder();
for(int i=0; i<bulletList.size(); i++){
CharSequence bulletLine = bulletList.get(i) + (i < bulletList.size()-1 ? "\n" : "");
Spannable spannable = new SpannableString(bulletLine);
String bulletPointStyle = bulletStyle.getCode();
LeadingMarginSpan bulletLeadingMarginSpan = new LeadingMarginSpan()
{
@Override
public int getLeadingMargin(boolean first) {
return bulletPointStyle.length()*bulletGapWidth;
}
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {
if (first) {
Paint.Style orgStyle = p.getStyle();
p.setStyle(Paint.Style.FILL);
p.setColor(bulletColor);
c.drawText(bulletPointStyle + " ", 0, bottom - p.descent(), p);
p.setColor(Color.BLACK);
p.setStyle(orgStyle);
}
}
};
spannable.setSpan(bulletLeadingMarginSpan, 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
sbb.append(spannable);
}
return sbb;
}
}
或使用引线边缘span:
String text = "- Enhanced base performance. - Lightweight headband enhances comfort and adds durability - Easy to adjust headband ensures optimum fit and comfort - 2 metre-long cable";
bulletTextView.setText(BulletUtil.addBullets(text, "- ", 100, getResources().getColor(android.R.color.holo_red_dark)));
String text = "- Enhanced base performance. - Lightweight headband enhances comfort and adds durability - Easy to adjust headband ensures optimum fit and comfort - 2 metre-long cable";
bulletTextView.setText(BulletUtil.addLeadingBullets(text, "- ", 100, getResources().getColor(android.R.color.black), BulletUtil.BulletStyle.Style2));
我在上面的示例中使用了您的示例数据。您可以添加一个运行时的图像吗?