0

我的应用程序有以下问题:我创建了一个自定义视图类:

 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,但相同。

请帮我

4

1 回答 1

0

我也遇到了依赖于相对布局大小的相对布局中的视图问题。我通过更新内部视图大小解决了这个问题

private void updateViewSize()
if(rootContainer == null || rootContainer.getLayoutParams() == null)
return;
rootContainer.measure(View.MeasureSpec.makeMeasureSpec(ScalingUtil.getScreenSize().getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY));
RelativeLayout.LayoutParams oldLayoutParams = (RelativeLayout.LayoutParams) rootContainer.findViewById(R.id.deals_card_center_bg).getLayoutParams();
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(rootContainer.getMeasuredWidth(), rootContainer.getMeasuredHeight());
    layoutParams.setMargins(oldLayoutParams.leftMargin, oldLayoutParams.topMargin, oldLayoutParams.rightMargin, oldLayoutParams.bottomMargin);

    rootContainer.findViewById(R.id.deals_card_center_bg).setLayoutParams(layoutParams);
于 2014-01-23T14:57:25.900 回答