在我的布局中,我有一个包含自定义视图的相对布局:
<RelativeLayout
android:id="@+id/layout_sectionprogress"
android:layout_width="500dp"
android:layout_height="50dp"
android:layout_marginTop="25dp"
android:layout_centerHorizontal="true">
<com.example.mySilhouette.CustomView.CreateProfilProgress
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
我的自定义视图:
private Paint mCirclePaint = new Paint();
private float mCircleRadius=10f;
// constructor
public CreateProfilProgress(Context context) {
super(context);
init();
}
public void init() {
mCirclePaint.setColor(Color.argb(100,217,58,52));
mCirclePaint.setAntiAlias(true);
mCirclePaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
float centerX = this.getMeasuredWidth()/2;
float centerY = this.getMeasuredHeight()/2;
canvas.drawCircle(centerX, centerY, mCircleRadius, mCircleOnPaint);
super.onDraw(canvas);
}
@Override
public void onSizeChanged (int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
// Try for a width based on our minimum
int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
int w = resolveSizeAndState(minw, widthMeasureSpec, 1);
// Whatever the width ends up being, ask for a height that would let the pie
// get as big as it can
int minh = MeasureSpec.getSize(w) + getPaddingBottom() + getPaddingTop();
//int h = resolveSizeAndState(MeasureSpec.getSize(w) - (int)mTextWidth, heightMeasureSpec, 0);
int h = resolveSizeAndState(MeasureSpec.getSize(minh) , heightMeasureSpec, 0);
setMeasuredDimension(w, h);
}
在我的活动中,我设置了包含自定义视图的布局:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_user);
}
由于我不知道的原因,它不会显示。当我将这个自定义视图放在一个片段中并用这个自定义视图来扩展我的片段布局时,它很好地绘制,但不在我的活动中。有没有办法在活动中绘制自定义视图?