自定义形状不会出现在RelativeLayout中

时间:2013-03-21 14:41:44

标签: android view parent relativelayout

我的应用程序出现以下问题:我创建了一个自定义View类:

 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);
        }
    }

这个类是一个内部类,我试图在Button前加一个RelativeLayout。 我已成功尝试将其添加到另一个布局,因此可以正确添加和绘制(请参阅此处screenshot)。

在前一种情况下,调用绘制函数并绘制半圆。

以下虽然没有调用绘图功能 .. 我使用这段代码:

ViewGroup p=(ViewGroup)button.getParent();
MyCustomPanel c=new MyCustomPanel(p.getContext(),p.getWidth(),p.getHeight());
((ViewGroup)p.findViewById(R.id.semicircleFrame)).addView(c);

并且RelatedLayout中的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但是相同。

请帮帮我

1 个答案:

答案 0 :(得分: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);