2013-09-28 98 views
19

我想在textview上放一个圆圈背景。渲染时,圆形变成椭圆形。Android圈背景变成椭圆形

我的布局XML:

<TextView 
     android:id="@+id/amount_key" 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_marginRight="2dp" 
     android:gravity="center" 
     android:background="@drawable/circle" 
     android:layout_marginLeft="20dp" 
     android:text="3\ndays" 
     android:padding="20dp" 
     android:textColor="#ffffff" 
     android:textStyle="bold" 
     android:textSize="25dp" /> 


</LinearLayout> 

我的圈子背景:

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

      <solid 
       android:color="#79bfea"/> 
</shape> 
+0

您应该设置layout_weight和layout_height固定值,我认为。下面的@ sudhasri的解决方案不起作用? –

+0

我做到了这一点,我通过扩展TextView来设置宽度为高度也覆盖onMeasure –

回答

26
  • 更改textViewlayout_heightlayout_widthwrap_content
  • 添加尺寸标签形状标签中如下
<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval">> 
<solid android:color="#79bfea" /> 
<size android:height="25dp" 
    android:width="25dp"/> 
</shape> 

如果它仍然是椭圆形的,尝试增加宽度和身高标签。它为我工作!

+0

在我的情况'layout_height'和'layout_width'到'wrap_content'并调整rigth填充的技巧。 –

+0

@GueorguiObregon是以文本为中心的吗? – Sudhasri

2

试环,而不是椭圆形

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

    <solid android:color="#79bfea" /> 
</shape> 
+8

这实际上并没有显示任何东西。 –

1

由于您使用match_parent的宽度和高度,在后台设置drawable,所以它w虐待是椭圆形的。要实现圆形,您可以为宽度和高度提供相同的尺寸。 NA不想全屏,那么你可以从java使用WindowManager代码的宽度,并设置宽度和高度相同的值。

3

我延伸的TextView类来设置宽度为高度

public class TextViewSquareShaped extends TextView { 


public TextViewSquareShaped(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(attrs); 
} 

public TextViewSquareShaped(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(attrs); 

} 

public TextViewSquareShaped(Context context) { 
    super(context); 
    init(null); 
} 

protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
} 

private void init(AttributeSet attrs) { 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); 
    setMeasuredDimension(width, width); 
    Log.v("measure", "width:" + width + " height:" + width); 
} 
} 

,然后与上述Sudhasri的回答,我可以显示精确圆形文本图。

所以没有必要给予固定的身高和体重。

1

为了得到这个

enter image description here

我用了两个LinearLayout中彼此并设置父重力里面CENTER

<LinearLayout 
android:layout_width="80dp" 
android:layout_height="50dp" 
android:baselineAligned="false" 
android:gravity="center" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="45dp" 
    android:layout_height="45dp" 
    android:gravity="center" 
    android:background="@drawable/oval" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Text" 
     android:textSize="12sp" /> 
</LinearLayout> 
</LinearLayout> 

,这是oval.xml绘制文件夹内

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <solid android:color="#393939" /> 
    <size 
     android:width="40dp" 
     android:height="40dp" /> 
</shape> 
没有内部LinearLayout的

你会得到这个

enter image description here

它的代码是

<LinearLayout 
android:layout_width="80dp" 
android:layout_height="50dp" 
android:baselineAligned="false" 
android:gravity="center" 
android:background="@drawable/oval" 
android:orientation="vertical"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="Text" 
    android:textSize="12sp" /> 
</LinearLayout> 
10

您可以创建自己的绘制对象,这将限制半径其宽度和高度之间的最小值。

package com.example.android; 

import android.graphics.Canvas; 
import android.graphics.ColorFilter; 
import android.graphics.Paint; 
import android.graphics.PixelFormat; 
import android.graphics.Rect; 
import android.graphics.drawable.Drawable; 

public class ColorCircleDrawable extends Drawable { 
    private final Paint mPaint; 
    private int mRadius = 0; 

    public ColorCircleDrawable(final int color) { 
     this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     this.mPaint.setColor(color); 
    } 

    @Override 
    public void draw(final Canvas canvas) { 
     final Rect bounds = getBounds(); 
     canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius, mPaint); 
    } 

    @Override 
    protected void onBoundsChange(final Rect bounds) { 
     super.onBoundsChange(bounds); 
     mRadius = Math.min(bounds.width(), bounds.height())/2; 
    } 

    @Override 
    public void setAlpha(final int alpha) { 
     mPaint.setAlpha(alpha); 
    } 

    @Override 
    public void setColorFilter(final ColorFilter cf) { 
     mPaint.setColorFilter(cf); 
    } 

    @Override 
    public int getOpacity() { 
     return PixelFormat.TRANSLUCENT; 
    } 
} 

然后应用到你的TextView:

textView.setBackground(new ColorCircleDrawable(Color.RED)); 
+0

要得到一个圆环而不是一个实心圆,请使用paint.setStyle(Paint.Style.STROKE)和paint.setStrokeWidth(strokeWidth)。 –

+0

请确保像这样传递颜色:int colorToUse = ContextCompat.getColor(getContext(),R.color.my_color); textView.setBackground(new ColorCircleDrawable(colorToUse));而不是textView.setBackground(new ColorCircleDrawable(R.color.my_color)); – PedroHidalgo