【问题标题】:Android draw circle around TextAndroid在文本周围画圈
【发布时间】:2021-03-06 07:14:19
【问题描述】:

我有一个 textView,其中包含一个字符(所有单个数字,数字 0-9)。我想在数字周围画一个圆圈或一个正方形。我看到一个线程提到使用九个补丁来围绕它进行样式设置,但我不确定如何做到这一点(或者它是否是最好的方法)。我怎样才能在数字周围有一个圆圈?

谢谢

【问题讨论】:

    标签: android textview


    【解决方案1】:

    只需要像这样创建一个圆形可绘制对象

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="oval">
        <corners android:radius="10dip"/>
        <stroke android:color="@color/red" android:width="2dip"/>
        <solid android:color="@android:color/transparent"/>
    </shape>
    

    并将这个drawable设置为你的TextView背景。

    【讨论】:

    • 为什么在椭圆形内使用锥体?来自文档: 为形状创建圆角。仅在形状为矩形时适用。
    • 绘制椭圆而不是圆形
    • 在 textView 中添加比顶部和底部稍多的左右填充
    【解决方案2】:
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <stroke android:color="@color/teal_200" android:width="2dip"/>
        <solid android:color="@android:color/transparent"/>
        <size
            android:width="10dp"
            android:height="10dp" 
        />
    </shape>
    

    【讨论】:

      【解决方案3】:

      您应该能够只给 TextView 一个 Shape drawable,它是一个圆形作为背景。

      http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-05
        • 1970-01-01
        • 2021-04-11
        • 1970-01-01
        • 2013-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多