【问题标题】:Showing contact name with initial letter in circle在圆圈中显示带有首字母的联系人姓名
【发布时间】:2015-08-07 07:26:55
【问题描述】:

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

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

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我试图对其进行定制,但在为棒棒糖制作应用程序时正在寻找材料设计指南。
  • 我投票结束这个问题,因为我们不是代码编写服务。您应该尽最大努力自己解决问题,然后在遇到具体问题时提出问题。
  • 所有这些都是有用的,但缺少的部分是它们应该显示一个头像是可用的,如果不可用,则显示字母。

标签: android


【解决方案1】:

您可以关注this.,如果您想节省时间,可以使用this 库。

编辑

链接可能随时无效。所以我想在下面添加细节。

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);    

该库支持circlesquarerectangle 可绘制对象。

【讨论】:

  • 虽然这可能是一个很好的答案,但它本质上是一个“仅链接”的答案。您应该包含链接中的一些信息,以便如果链接背后的信息曾经更改/丢失,答案仍然有意义。见this link
  • 感谢您的建议@JamesWebster。
  • @ImMathan 这是一个更好的答案。我已经推翻了我的反对票和赞成票
  • @JamesWebster 太棒了:)
  • 谢谢老板 :) 它帮助了我,实际上我没有多少时间去做这件事,所以它节省了我的时间。再次感谢:)
【解决方案2】:

我没有看到任何库,但我的解决方案是在运行时在你的 xml 中的预定义布局中画一个圆圈,使用这个: How to draw circle by canvas in Android?

然后在您的列表适配器中使用 substring(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>

你在drawable文件夹中的形状为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 中将其用作要充气的项目。like this

【讨论】:

  • 你能分享一下代码吗?
  • @AlokRajasukumaran Rajasukumaran 检查我提供的更新。
【解决方案3】:

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

<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"
        />

【讨论】:

    【解决方案4】:

    通过使用 Material Card View 和 Textview,很容易创建这样的视图。

     <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 2018-10-18
      相关资源
      最近更新 更多