android - 如何制作像 Gmail 中的首字母缩写图标?

标签 android xml view

我想做的事:

enter image description here

我有:

enter image description here

我想制作一个带有缩写的圆形图标。在我的场景中,首字母缩写可以包含一两个字母。就像你可以看到我的解决方案适用于两个字母,但不适用于一个字母。对于这两种情况我该如何解决?

这是 XML 文件中的我的代码:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="20dp"
    android:textAllCaps="true"
    android:textColor="@color/white"
    android:background="@drawable/shape_rounded_blue_button"
    android:layout_gravity="center_horizontal"
    android:gravity="center" 
    local:MvxBind="Text Initials"/>

还有 shape_rounded_blue_button:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/blue"/>
<corners android:radius="30dp" />
</shape>

最佳答案

因为我想实现同样的事情,所以我在 GitHub 上做了一些研究并找到了这个库: https://github.com/amulyakhare/TextDrawable

它完全符合您的要求,并且具有自动着色等一些简洁的功能。您可以将它与 ImageView 一起使用并创建一个可绘制对象,例如像这样:

val drawable = TextDrawable.builder()
        .beginConfig()
        .width(bubbleSize)
        .height(bubbleSize)
        .endConfig()
        .buildRound("FH", MATERIAL.getColor("FH"))
theImageView.setImageDrawable(drawable)

看起来像这样: Round Image View

您的文本将自动在气泡内居中,因此包含一两个字符没有问题。

关于android - 如何制作像 Gmail 中的首字母缩写图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48127266/

相关文章:

android - 如何使用 AudioManager.STREAM_VOICE_CALL 更改通话音量

c# - XmlNodeList 上的 Any() linq 查询

java - 使用 dom4j 从流中读取单个 XML 文档

c# - 以标准方式访问没有值(value)的短信 Web 服务中的子 XML 值

python - Python/Django 中 PHP "ob"函数的等效函数

android - 如何格式化 android-range-seek-bar 的最小值和最大值?

android - 如何使用抽屉导航打开和关闭更改我的按钮图像

android - flutter 热重新加载在android studio(mac)中不起作用

Android:更改 View 顺序会导致 "Unfortunately, app has stopped"OK。为什么?

date - CouchDB 不更新即将发生和过去的事件的 View