2013-09-28 99 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