【问题标题】:Android: Creating a Circular TextView?Android:创建一个圆形 TextView?
【发布时间】:2014-08-08 12:26:46
【问题描述】:

下面是我当前的简单 XML,但是我希望其中的 3 个 TextView 是圆形的,而不是矩形的。

如何更改我的代码?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvSummary1"
        android:layout_width="270dp"
        android:layout_height="60dp" >
    </TextView>

    <TextView
        android:id="@+id/tvSummary2"
        android:layout_width="270dp"
        android:layout_height="60dp" >
    </TextView>

    <TextView
        android:id="@+id/tvSummary3"
        android:layout_width="270dp"
        android:layout_height="60dp" >
    </TextView>

</LinearLayout>

注意:我想要一个实际的 圆形,而不是如下所示的弯曲边缘矩形:

编辑:

当前代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvSummary1"
        android:layout_width="270dp"
        android:layout_height="60dp"
        android:text=" " 
        android:gravity="left|center_vertical"
        android:background="@drawable/circle"/>

    <TextView
        android:id="@+id/tvSummary2"
        android:layout_width="270dp"
        android:layout_height="60dp"
        android:background="@drawable/circle" >
    </TextView>

    <TextView
        android:id="@+id/tvSummary3"
        android:layout_width="270dp"
        android:layout_height="60dp"
        android:background="@drawable/circle" >
    </TextView>

</LinearLayout>

当前输出:

【问题讨论】:

  • 请简要介绍您的问题。
  • 你想要TextView吗?发布一些屏幕截图,以便了解您的要求。
  • 我只希望布局中的每个文本视图都是圆形
  • 为什么要质疑-2???,我在找同样的东西

标签: java android textview xml-layout


【解决方案1】:

我也在寻找解决这个问题的方法,并且我发现简单舒适的方法是将矩形 TextView 的形状转换为圆形。有了这个方法就完美了:

  1. 在名为“circle.xml”的drawable文件夹中创建一个新的XML文件(例如)并用以下代码填充它:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    
        <solid android:color="#9FE554" />
    
        <size
            android:height="60dp"
            android:width="60dp" />
    
    </shape>
    

使用此文件,您将创建新形式的 TextView。在这种情况下,我创建了一个绿色圆圈。如果要添加边框,则必须将以下代码添加到上一个文件中:

    <stroke
        android:width="2dp"
        android:color="#FFFFFF" />
  1. 使用以下代码在可绘制文件夹中创建另一个 XML 文件(“rounded_textview.xml”):

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/circle" />
    </selector>
    

这个文件将用于改变我们eligamos的TextView的方式。

  1. 最后,在我们要更改方式的 TextView 属性部分,我们前往“背景”并选择创建的第二个 XML 文件(“rounded_textview.xml”)。

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="H"
        android:textSize="30sp"
        android:background="@drawable/rounded_textview"
        android:textColor="@android:color/white"
        android:gravity="center"
        android:id="@+id/mark" />
    

通过这些步骤,TextView 不再是 TextView,而是矩形有一个圆形。只需更改形状,而不是 TextView 的功能。结果如下:

另外我不得不说,这些步骤可以应用于任何其他在属性中具有“背景”选项的组件。

好运!!

【讨论】:

  • 这很好用。你能告诉我如何在运行时设置背景颜色吗?似乎无法弄清楚。谢谢。
  • 这对我来说效果很好,但我不需要选择器文件。如果我想要不同的状态,我想它会派上用场。
【解决方案2】:

典型的解决方案是定义形状并将其用作背景,但随着位数的变化,它不再是一个完美的圆形,它看起来像一个带有圆边的矩形或椭圆形。所以我开发了这个解决方案,效果很好。希望它会对某人有所帮助。

这里是自定义TextView的代码

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

public class CircularTextView extends TextView
{
private float strokeWidth;
int strokeColor,solidColor;

public CircularTextView(Context context) {
    super(context);
}

public CircularTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CircularTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


@Override
public void draw(Canvas canvas) {

    Paint circlePaint = new Paint();
    circlePaint.setColor(solidColor);
    circlePaint.setFlags(Paint.ANTI_ALIAS_FLAG);

    Paint strokePaint = new Paint();
    strokePaint.setColor(strokeColor);
    strokePaint.setFlags(Paint.ANTI_ALIAS_FLAG);

    int  h = this.getHeight();
    int  w = this.getWidth();

    int diameter = ((h > w) ? h : w);
    int radius = diameter/2;

    this.setHeight(diameter);
    this.setWidth(diameter);

    canvas.drawCircle(diameter / 2 , diameter / 2, radius, strokePaint);

    canvas.drawCircle(diameter / 2, diameter / 2, radius-strokeWidth, circlePaint);

    super.draw(canvas);
}

public void setStrokeWidth(int dp)
{
    float scale = getContext().getResources().getDisplayMetrics().density;
    strokeWidth = dp*scale;

}

public void setStrokeColor(String color)
{
    strokeColor = Color.parseColor(color);
}

public void setSolidColor(String color)
{
    solidColor = Color.parseColor(color);

}
}

然后在您的 XML 中,添加一些填充并确保其重心位于中心

<com.app.tot.customtextview.CircularTextView
        android:id="@+id/circularTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="11"
        android:gravity="center"
        android:padding="3dp"/>

并且可以设置笔画宽度

circularTextView.setStrokeWidth(1);
circularTextView.setStrokeColor("#ffffff");
circularTextView.setSolidColor("#000000");

【讨论】:

  • 我很高兴它有帮助:)
  • 如果数字
  • @umerk44 这是一个很好的问题解决方案,我唯一遇到的问题是,当它显示时,大约一半的圆圈暂时没有显示。对这个问题有什么建议吗?
  • @NickUnuchek 它总是 wrap_content,如果你看到数字周围有太多空间,请删除填充
  • 为什么要覆盖draw,而不是onDraw?在 draw/onDraw 中创建对象似乎是一种不好的做法,因为它经常被触发并且我们有 GC 事件的风险,这将冻结一切。
【解决方案3】:

创建一个 texview_design.xml 文件并使用以下代码填充它。把它放在 res/drawable 中。

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

        <solid android:color="#98AFC7" />

        <stroke
            android:width="2dp"
            android:color="#98AFC7" />

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

    </shape>

然后在您的主 XML 文件中为每个 TextView 添加以下行:

  android:background="@drawable/texview_design"

第二种方式(不推荐): 下载这个圈子并将其放在您的drawable 文件夹中,然后将其设为您的TextView's 背景。然后将gravity 设置为center

那么它会是这样的:

【讨论】:

  • 我只是用我的按钮和对话框做到了,所以我确定。
  • 等待.. 那行没有作为代码添加.. 只是编辑。我的错。现在只需运行它,看看它是否符合您的要求。如果确实如此,则选择此答案。 :)
  • 太棒了。现在您可以根据您的要求更改颜色和圆度(在设计 xml 文件中)。 :)
  • 这不是创建一个圆,而是创建一个带有弯曲边缘的矩形?
  • muchas gracias fue de mucha utilidad
【解决方案4】:

这是一个防止椭圆形背景变成圆形的矩形。
将视图设为正方形将解决所有问题。

我发现这个解决方案很干净,适用于不同的文本大小和文本长度。

public class EqualWidthHeightTextView extends TextView {

    public EqualWidthHeightTextView(Context context) {
        super(context);
    }

    public EqualWidthHeightTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EqualWidthHeightTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int r = Math.max(getMeasuredWidth(),getMeasuredHeight());
        setMeasuredDimension(r, r);

    }
}


用法

<com.commons.custom.ui.EqualWidthHeightTextView
    android:id="@+id/cluster_count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="10+"
    android:background="@drawable/oval_light_blue_bg"
    android:textColor="@color/white" />


oval_light_blue_bg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"<br>
       android:shape="oval">
   <solid android:color="@color/light_blue"/>
   <stroke android:color="@color/white" android:width="1dp" />
</shape>

【讨论】:

  • 这是我能找到的唯一正确答案。
  • 这应该是公认的答案。像魅力一样工作
  • 任何小于 10 的文本都向右对齐,即使我们设置 android:gravity="center" 添加android:inputType="number" 将使文本居中对齐,即使文本小于 10。
  • 加上直接的答案
  • 它只是 rox,最干净、最有效的解决方案!
【解决方案5】:

1.使用以下代码在您的 res/drawable 文件夹中创建一个 xml circle_text_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
     <solid android:color="#fff" />

    <stroke
        android:width="1dp"
        android:color="#98AFC7" />
    <corners
         android:bottomLeftRadius="50dp"
         android:bottomRightRadius="50dp"
         android:topLeftRadius="50dp"
         android:topRightRadius="50dp" />
    <size
         android:height="20dp"
         android:width="20dp" />
</shape>

2.使用 circle_text_bg 作为你的文本视图的背景。注意:为了得到一个完美的圆圈,你的 textview 高度和宽度应该是相同的。Preview of what your textview with text 1, 2, 3 with this background should look like this

【讨论】:

  • 当使用这个选项时,记得添加android:gravity="center"到你的TextView
【解决方案6】:

这是我真正有效的解决方案

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <!-- The fill color -->
    <solid android:color="#ffff" />
    <!-- Just to add a border -->
    <stroke
        android:color="#8000"
        android:width="2dp"
    />
</shape>

如果您想要一个完美(未拉伸)的圆圈,请确保您的 TextView 宽度和高度匹配(在 dp 中相同)。

通过缩短文本或放大圆圈或缩小文本大小或减小填充(如果有)来确保文本适合圆形。 或以上建议的组合。

[编辑]

对于我在您的图片中看到的内容,您想在一行上添加太多文字,对于纯圆圈。
考虑到文本应该有一个square 方面,所以你可以将它包裹起来(使用\n)或者只是将数字放在圆圈内,然后将文字放在相应的圆圈上方或下方。 p>

【讨论】:

  • 我下一条评论中的这段代码对 TextView 是否正确?注意我称你的代码为“circle 2”。这段代码不是在做一个圆,而是在做一个带有弯曲边缘的矩形吗?
  • 它正在为我画出完美的圆圈。我将宽度和高度设置为 32 dp,将文本设置为 16sp。但它应该没有什么区别,通过改变大小。
  • 通过缩短文本或扩大圆圈或缩小文本大小,确保文本适合圆圈。
  • 对于我在您的图片中看到的内容,您想为纯圆圈添加太多文字。考虑到文本应该有一个“方形”的方面,所以你可以将它包裹起来(使用 \n)或者只是把数字放在里面,然后把文字放在圆圈上面或下面。
【解决方案7】:

你可以在drawable文件夹的round_tv.xml试试这个:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <stroke android:color="#22ff55" android:width="3dip"/>

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

    <size
        android:height="60dp"
        android:width="60dp" />

</shape>

在您的文本视图中应用该可绘制对象:

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/round_tv"
    android:gravity="center_vertical|center_horizontal"
    android:text="ddd"
    android:textColor="#000"
    android:textSize="20sp" />

输出:

希望这会有所帮助。

编辑:如果你的文字太长,椭圆形更受欢迎。

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

    <stroke android:color="#55ff55" android:width="3dip"/>

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

    <size
        android:height="60dp"
        android:width="60dp" />

</shape>

输出:

如果你还需要一个合适的圆圈,那么我猜你需要在设置文本后动态设置它的高度,新的高度应该和它的新宽度一样多,以便形成一个合适的圆圈。

【讨论】:

  • 这不是创建一个圆,而是创建一个带有弯曲边缘的矩形?
  • @codeme2020 可能是文本大小和宽度的问题。请参阅更新答案中的屏幕截图。
  • 谢谢,如何更改我的文本大小和宽度以确保它是一个圆圈
  • @codeme2020 文本已修复?还是会动态决定?
  • 文本是动态添加的
【解决方案8】:

在你的drawable中使用它

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


    <solid android:color="#55ff55"/>


    <size android:height="60dp"
        android:width="60dp"/>

</shape>

将textview的背景设置为这样

【讨论】:

  • 为什么要用矩形?
  • 为什么不使用合适的形状(椭圆形)?
【解决方案9】:

这里的大部分答案似乎是对可绘制形状的破解,而 android 本身通过形状功能支持这一点。这对我来说非常有效。你可以通过两种方式做到这一点

使用固定的高度和宽度,这将保持不变 不管你放什么文字如下所示

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="@color/alpha_white" />

    <size android:width="25dp" android:height="25dp"/>

<stroke android:color="@color/color_primary" android:width="1dp"/>

</shape>

使用 Padding 重新调整形状,而不考虑文本 textview如下图所示

<solid android:color="@color/alpha_white" />

<padding
    android:bottom="@dimen/semi_standard_margin"
    android:left="@dimen/semi_standard_margin"
    android:right="@dimen/semi_standard_margin"
    android:top="@dimen/semi_standard_margin" />

<stroke android:color="@color/color_primary" android:width="2dp"/>

semi_standard_margin = 4dp

【讨论】:

    【解决方案10】:

    我使用:/drawable/circulo.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
        <solid android:angle="270"
            android:color="@color/your_color" />
    
    </shape>
    

    然后我在我的 TextView 中使用它:

    android:background="@drawable/circulo"
    

    无需复杂化。

    【讨论】:

    • 很好的解决方案!
    • 直接回答,谢谢!!
    【解决方案11】:

    试试下面的可绘制文件。在您的res/drawable 文件夹中创建名为“circle”的文件并复制以下代码:

       <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval" >
    
        <solid android:color="#FFFFFF" />
    
        <stroke
            android:width="1dp"
            android:color="#4a6176" />
    
        <padding
            android:left="10dp"
            android:right="10dp"
            android:top="10dp"
            android:bottom="10dp"
             />
    
        <corners android:radius="10dp" />
    
    </shape>
    

    将其应用到您的TextView 中,如下所示:

    <TextView
            android:id="@+id/tvSummary1"
            android:layout_width="270dp"
            android:layout_height="60dp"
            android:text="Hello World" 
            android:gravity="left|center_vertical"
            android:background="@drawable/round_bg">
    
        </TextView>
    

    【讨论】:

    • 这不是创建一个圆,而是创建一个带有弯曲边缘的矩形?
    • @codeme2020 试试oval 看看我更新了我的选择器。这是你想要的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    相关资源
    最近更新 更多