2

我想实现这样的目标:

在此处输入图像描述

如果我们谈论被包围的那个。我猜25这将是 TextView2并将成为该文本视图的背景图像。这里,2表示当天的事件数。但是事件的数量可以是动态的。

如何创建带有动态文本的背景形状?是否可以?

编辑

Mission Accomplished:-)

通过扩展NPike 的代码,我实现了所需的设计。虽然我开始在我的脑海里有别的东西。但在工作的过程中,我改变了主意,现在我认为我的设计比我最初计划的要好。

这是我现在的设计:

在此处输入图像描述

这是我的代码:

导入android.content.Context;导入android.graphics.Canvas;导入android.graphics.Color;导入android.graphics.Paint;导入android.graphics.Typeface;导入android.util.AttributeSet;导入 android.widget.TextView;

public class MonthlyCalTextView extends TextView {
    private Paint mPaint;
    private String strNoOfEvents;


    public MonthlyCalTextView(Context context, AttributeSet attrs, String noOfEvents) {
        super(context, attrs); 

        strNoOfEvents = ( Integer.parseInt(noOfEvents)>100 )? "100+":noOfEvents;
        strNoOfEvents = (strNoOfEvents.equals("0")) ? "":strNoOfEvents;
        mPaint = new Paint();
        mPaint.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD) );
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.rgb(223, 223, 223));
    }

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


    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setTextSize(35);
        canvas.drawText(strNoOfEvents, getMeasuredWidth()-mPaint.measureText(strNoOfEvents)-3, getMeasuredHeight()-3, mPaint);


        super.onDraw(canvas);
    }

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

        // grow view dimensions to account for circle
        setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
    }

}

这就是我所说的:

Random rand = new Random();
// Just for demo assuming that the no. of events will be between 0-125
int min=0, max=125;
String randomNum = String.valueOf( rand.nextInt(max - min + 1) + min );
MonthlyCalTextView tvData=new MonthlyCalTextView(CalendarMonthlyFragment.activity, null, randomNum);
4

2 回答 2

3

尝试这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true" >

<RelativeLayout
    android:layout_width="10dp"
    android:layout_height="10dp"
    android:layout_centerInParent="true"
    android:layout_margin="5dp"
    android:background="@drawable/sample_image" >

    <TextView
        android:id="@+id/some_title"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        android:gravity="center"
        android:textColor="@android:color/white" />
</RelativeLayout>

并在 java 类中将相对布局膨胀为一个视图,并将 textView 膨胀为膨胀视图的 findById。

这是本机日历应用程序的方式。显示今日视图。

于 2013-02-07T18:28:25.817 回答
3

您可以创建自己的 TextView 扩展,覆盖 onDraw 以绘制圆。

这是一个简短的例子:

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

public class CircleTextView extends TextView {

    private static final int PADDING = 15;
    private Paint mCirclePaint;


    public CircleTextView(Context context, AttributeSet attrs) {
        super(context, attrs); 

        mCirclePaint = new Paint();
        mCirclePaint.setStyle(Style.FILL);
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(Color.GREEN); 
    }


    @Override
    protected void onDraw(Canvas canvas) {
        // draw circle at center of canvas
        canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, PADDING, mCirclePaint);
        super.onDraw(canvas);
    }

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

        // grow view dimensions to account for circle
        setMeasuredDimension(resolveSize(getMeasuredWidth()+(PADDING*2), widthMeasureSpec), resolveSize(getMeasuredHeight()+(PADDING*2), heightMeasureSpec));
    }

}
于 2013-02-07T18:40:47.717 回答