2013-03-21 27 views
0

我有以下问题,我的应用程序:我创建了一个自定义的视图类:自定义形状不会出现在RelativeLayout的

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添加在Button前面。 我已经成功尝试将其添加到另一个布局,因此它被正确添加并绘制(请参见screenshot)。

在前面的例子中,调用draw函数并绘制半圆。

在下面虽然绘制函数不叫 .. 我用这个代码:

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

是否检查'p.getWidth()'和'p.getHeight()'返回值大于零?如果在第一次度量/布局过程之前调用这些方法,它们将返回零。 HierarchyViewer应该显示您的自定义面板的测量/实际宽度和高度。 – Karakuri 2013-03-21 14:44:13

+0

为了阐明@Karakuri所说的话,你应该正确地实现'onMeasure()',参见[documentation](http://developer.android.com/guide/topics/ui/custom-components.html#custom)。 – adrianp 2013-03-21 14:49:52

+0

我尝试手动插入值(比contener视图更小的宽度和长度)并没有任何反应。 如果您注意到我正在使用Log.d(“DEBUG”,“R =”+ radius +“cx =”+ center_x +“cy =”+ center_y);'以查看draw函数是否被调用。但在有问题的情况下,这条线从来没有达到! – Pontios 2013-03-21 16:20:05

回答

0

我也有与相对布局的规模thatwas依赖性相对布局视图的问题。 我有问题解决了这个问题,通过更新内部视图大小

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