首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Textdrawable库中添加前两个字符?

如何在Textdrawable库中添加前两个字符?
EN

Stack Overflow用户
提问于 2019-05-24 18:59:01
回答 1查看 801关注 0票数 0

您好,朋友,我正在使用compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' this library in my project i want add first two character inTextDrawableicon if anyone know about this library please help how add first two character of my text inTextDrawableicon here is my code my it show only first character of myTextViewinTextDrawable`图标,但我想要它显示前两个字符,请帮助我?

代码语言:javascript
运行
复制
//get first letter of each String item
String firstLetter = String.valueOf(getItem(position).charAt(0));

ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
// generate random color
int color = generator.getColor(getItem(position));

TextDrawable drawable = TextDrawable.builder().buildRound(firstLetter,//radius in px color);

我的xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp">

    <ImageView
        android:id="@+id/image_view"
        android:src="@drawable/ic_home_black_24dp"
        android:layout_width="40dp"
        android:layout_height="40dp" />

    <TextView
        android:id="@+id/text"
        android:paddingLeft="20sp"
        android:textSize="22sp"
        android:fontFamily="sans-serif"
        android:textColor="@color/darkcolor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="John" />
</LinearLayout>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-24 19:10:06

使用子字符串()而不是chaAt()

代码语言:javascript
运行
复制
String firstTwoChars = getItem(position).subString(0,2); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56291279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档