Android 在圆圈中显示联系人姓名和首字母

Android 在圆圈中显示联系人姓名和首字母,android,Android,我需要在圆圈内显示联系人姓名的首字母,就像棒棒糖联系人一样。我没有找到任何解决办法 圆圈应该为每个用户提供不同的颜色。如果您想节省时间,可以使用图书馆 编辑 链接在任何时间点都可能无效。因此,我想在下面添加详细信息 repositories{ maven { url 'http://dl.bintray.com/amulyakhare/maven' } } dependencies { compile 'com.amulyakhare:com.amulyakhare.tex

我需要在圆圈内显示联系人姓名的首字母,就像棒棒糖联系人一样。我没有找到任何解决办法

圆圈应该为每个用户提供不同的颜色。

如果您想节省时间,可以使用图书馆

编辑

链接在任何时间点都可能无效。因此,我想在下面添加详细信息

repositories{
  maven {
    url 'http://dl.bintray.com/amulyakhare/maven'
  }
}

dependencies {
  compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
}    
您可以添加所遵循的方法

基本上,您必须添加一个高度和宽度相同的
ImageView
,并添加此
TextDrawable
作为视图的背景

TextDrawable drawable = TextDrawable.builder()
            .buildRect("A", Color.RED);
ImageView image = (ImageView) findViewById(R.id.image_view);
image.setImageDrawable(drawable);    

该库支持
圆形
方形
矩形
可绘图项。

我没有看到任何用于此的库,但我的解决方案是在运行时在xml中的预定义布局中绘制圆形。使用以下方法:

然后在列表中,适配器使用子字符串(0,1)选择每个名称字符串的第一个字母 ,并使用setText()将列表项textView设置为第一个字母。 如果您需要源代码,请让我为您提供

更新: 我最近在我的一个应用程序中使用了非常简单的方法, 您应该在布局文件夹中创建如下布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/rlWeekDay"
  android:layout_width="45dp"
  android:layout_height="45dp"
  android:layout_gravity="right"
  android:layout_margin="3dp"
  android:background="@drawable/circle_shape">

<TextView
    android:id="@+id/tvWeekDayFirstLetter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text=" E "
    android:textStyle="bold"
    android:textColor="@color/colorPrimary"
    android:textSize="20sp" />
</RelativeLayout>

可绘制文件夹中的形状为circle_shape.xml:

   <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <item>
         <shape android:shape="oval"
            android:dither="true">

            <corners
            android:bottomRightRadius="100dp"
            android:bottomLeftRadius="100dp"
            android:topLeftRadius="100dp"
            android:topRightRadius="100dp"/>

        <solid android:color="#ccc" />

    </shape>
</item>
<item android:bottom="3dp">
<shape android:shape="oval"
    android:dither="true">
<solid android:color="@color/white"/> <!-- this one is ths color of the  Rounded Button -->
<corners
    android:bottomRightRadius="100dp"
    android:bottomLeftRadius="100dp"
    android:topLeftRadius="100dp"
    android:topRightRadius="100dp"/>



 </shape>
   </item>
     </layer-list>


然后在您的listView或recyclerview中,将其用作充气的物品。类似于此

执行此操作的简单小代码-

<com.google.android.material.button.MaterialButton
        android:id="@+id/"
        android:layout_width="40dp"
        android:layout_height="50dp"
        android:text="A"
        android:clickable="false"
        app:backgroundTint="@color/Blue200"
        android:padding="0dp"
        app:cornerRadius="50dp"
        />

通过使用材质卡视图和文本视图,可以轻松创建此类视图

 <com.google.android.material.card.MaterialCardView
        android:layout_width="50dp"
        android:layout_height="50dp"
        app:cardBackgroundColor="@color/red"
        app:cardCornerRadius="50dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/initialTv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="AB" />

    </com.google.android.material.card.MaterialCardView>


到目前为止您尝试了什么?我试图定制它,但在为棒棒糖制作应用程序时,我正在寻找材料设计方面的指导原则。我投票结束这个问题,因为我们不是代码编写服务。你应该尽最大努力自己解决问题,然后在遇到具体问题时问一个问题。所有这些都是有用的,但缺少的部分是,它们应该显示一个化身是可用的,如果不是,则显示字母。虽然这可能是一个很好的答案,但本质上是一个“链接”回答。您应该包含一些链接中的信息,这样,如果链接后面的信息曾经更改/丢失,那么答案仍然是有意义的。请参见“谢谢你的建议”@JamesWebster@这是一个更好的答案。我已经扭转了我的否决票,并且upvoted@JamesWebster太好了:)谢谢你,博斯:)这对我很有帮助,事实上我做这件事的时间非常少,所以节省了我的时间。再次感谢:)你能分享一下代码吗,这样它会更有帮助吗?@AlokRajasukumaran-Rajasukumaran检查我提供的更新。