我的应用程序有以下问题:我创建了一个自定义视图类:
private class MyCustomPanel extends View {
private int width;
private int height;
public MyCustomPanel(Context context,int width,int height)
{
super(context);
this.height=height;
this.width=width;
}
@Override
public void draw(Canvas canvas) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(getResources().getColor(R.color.pending));
float radius=(height)/2;
float center_x = (width)/2;
float center_y = (height)/2;
Log.d("DEBUG","R="+radius+" cx="+center_x+" cy="+center_y);
final RectF oval = new RectF();
oval.set(center_x- radius,
center_y - radius,
center_x + radius,
center_y + radius);
//Draw a left semicircle
canvas.drawArc(oval, 90, 180, false, paint);
}
}
这个类是一个内部类,我试图在它RelativeLayout
前面添加一个按钮。我已成功尝试将其添加到另一个布局,因此它被正确添加和绘制(请参见此处的屏幕截图)。
在前一种情况下,调用 draw 函数并绘制半圆。
在下面虽然没有调用绘图函数..我使用这个代码:
ViewGroup p=(ViewGroup)button.getParent();
MyCustomPanel c=new MyCustomPanel(p.getContext(),p.getWidth(),p.getHeight());
((ViewGroup)p.findViewById(R.id.semicircleFrame)).addView(c);
和相关布局的 XML 是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
android:id="@+id/frame">
<Button
android:id="@+id/button"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
<RelativeLayout
android:id="@+id/semicircleFrame"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
</RelativeLayout>
</RelativeLayout>
层次结构查看器显示以下内容: screenshot
我也尝试将其直接添加到父级frame
,但相同。
请帮我