我想做的事:
我有:
我想制作一个带有缩写的圆形图标。在我的场景中,首字母缩写可以包含一两个字母。就像你可以看到我的解决方案适用于两个字母,但不适用于一个字母。对于这两种情况我该如何解决?
这是 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)
您的文本将自动在气泡内居中,因此包含一两个字符没有问题。
关于android - 如何制作像 Gmail 中的首字母缩写图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48127266/