我想实现这样的目标:
如果我们谈论被包围的那个。我猜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);