您好朋友,我正在使用compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' this library in my project i want add first two character in
TextDrawable icon if anyone know about this library please help how add first two character of my text in
TextDrawable icon here is my code my it show only first character of my
TextView in
TextDrawable`图标,但我希望它显示前两个字符,请帮助我吗?
//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:
<?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>
最佳答案
使用substring()代替chaAt()
String firstTwoChars = getItem(position).subString(0,2);