Android:创build一个圆形的TextView?

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

我怎样才能改变我的代码呢?

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

电stream输出:

在这里输入图像说明

典型的解决scheme是定义形状,并将其用作背景,但是随着数字的变化,它不再是一个完美的圆形,它看起来像是圆形或椭圆形的矩形。 所以我开发了这个解决scheme,它工作得很好。 希望它能帮助别人。

圆形文本视图

 Here is the code of custom 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"); 

我也在寻找解决这个问题的办法,就像我发现的那样简单舒适,就是将矩形TextView的形状转换成圆形。 用这个方法将是完美的:

  1. 在名为“circle.xml”的可绘制文件夹(例如)中创build一个新的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> 

有了这个文件,你将创build新的TextViewforms。 在这种情况下,我创造了一个绿色的圆圈。 如果要添加边框,则必须将以下代码添加到前一个文件中:

  <stroke android:width="2dp" android:color="#FFFFFF" /> 
  1. 使用以下代码在可绘制文件夹中创build另一个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属性中,我们要改变方式部分,我们前往“背景”并select创build的第二个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 rectagular有一个圆形。 只是改变形状,而不是TextView的function。 结果如下:

在这里输入图像说明

此外,我不得不说,这些步骤可以应用到任何其他组件可以select在属性中的“背景”。

运气!!

创build一个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

那么它会看起来像这样:

在这里输入图像说明

我发现这个解决scheme是干净的,可以处理不同的文本大小和文本长度。

 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 h = this.getMeasuredHeight(); int w = this.getMeasuredWidth(); int r = Math.max(w,h); setMeasuredDimension(r, r); } } 

它可以防止椭圆形背景变得圆形。 使视图广场将解决一切。

 <com.commons.custom.ui.EqualWidthHeightTextView android:id="@+id/cluster_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" 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" android:shape="oval"> <solid android:color="@color/light_blue"/> <stroke android:color="@color/white" android:width="1dp" /> </shape> 

这是我实际工作的解决scheme

 <?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中相同),如果你想要一个完美的(未拉伸的)圆。

通过缩短文本或扩大圈子,或者缩小文本大小,或者减less填充(如果有的话),确保文本适合于一个圆圈。 或上述build议的组合。

[编辑]

对于我在图片中可以看到的内容,您希望在一行上添加太多的文字,对于纯粹的圆圈。
考虑到文本应该有一个正方形的方面,所以你可以包装它(使用\n ),或者只是把数字放在圆圈内,把文字放在相应的圆圈上面或者之前。

你可以在可绘制文件夹的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> 

在你的textviews中使用drawable:

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

输出:

在这里输入图像说明

如果你仍然需要一个适当的圆圈,那么我想你将需要在设置文本后dynamic设置它的高度,新的高度应该和它的新宽度一样多,以便形成一个适当的圆圈。

1.使用下面的代码在res / drawable文件夹中创build一个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的背景。 注意:为了得到一个完美的圆你的textview的高度和宽度应该是相同的。 用这个背景预览你的textview和text 1,2,3,应该是这样的

在你的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的背景

这里的答案似乎是可绘制形状的黑客,而Android本身支持这与形状function。 这对我来说是完美的。你可以用两种方法来做到这一点

使用一个固定的高度和宽度 ,不pipe你如下所示的文本,这将保持不变

 <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来重新调整形状,而不pipe文本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

我使用: /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" 

不需要复杂它。

试试下面的可绘制文件。 在res/drawable文件夹中创build一个名为“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> 

在这里输入图像说明