2011-07-12 99 views
2

我有一個自定義視圖如何將自定義視圖添加到佈局?

public GraphView(Context context, float[] values, String title, String[]horlabels, String[] verlabels, boolean type, int color) { 
    super(context); 
    if (values == null) 
     this.values = new float[0]; 
    else 
     this.values = values; 
    if (title == null) 
     title = ""; 
    else 
     this.title = title; 
    if (horlabels == null) 
     this.horlabels = new String[0]; 
    else 
     this.horlabels = horlabels; 
    if (verlabels == null) 
     this.verlabels = new String[0]; 
    else 
     this.verlabels = verlabels; 
    this.type = type; 
    paint = new Paint(); 
    this.color = color; 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    float border = 20; 
    float horstart = border * 2; 
    float height = getHeight(); 
    float width = getWidth() - 1; 
    float max = getMax(); 
    float min = getMin(); 
    float diff = max - min; 
    float graphheight = height - (2 * border); 
    float graphwidth = width - (2 * border); 

    paint.setTextAlign(Align.LEFT); 
    int vers = verlabels.length - 1; 
    for (int i = 0; i < verlabels.length; i++) { 
     paint.setColor(Color.DKGRAY); 
     float y = ((graphheight/vers) * i) + border; 
     canvas.drawLine(horstart, y, width, y, paint); 
     paint.setColor(Color.BLACK); 
     canvas.drawText(verlabels[i], 0, y, paint); 
    } 
    int hors = horlabels.length - 1; 
    for (int i = 0; i < horlabels.length; i++) { 
     paint.setColor(Color.DKGRAY); 
     float x = ((graphwidth/hors) * i) + horstart; 
     canvas.drawLine(x, height - border, x, border, paint); 
     paint.setTextAlign(Align.CENTER); 
     if (i==horlabels.length-1) 
      paint.setTextAlign(Align.RIGHT); 
     if (i==0) 
       paint.setTextAlign(Align.LEFT); 
     paint.setColor(Color.BLACK); 
     canvas.drawText(horlabels[i], x, height - 4, paint); 
    } 

    paint.setTextAlign(Align.CENTER); 
    canvas.drawText(title, (graphwidth/2) + horstart, border - 4, paint); 

    if (max != min) { 
     paint.setColor(color); 
     if (type == BAR) { 
      float datalength = values.length; 
      float colwidth = (width - (2 * border))/datalength; 
      for (int i = 0; i < values.length; i++) { 
       float val = values[i] - min; 
       float rat = val/diff; 
       float h = graphheight * rat; 
       canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint); 
      } 
     } else { 
      float datalength = values.length; 
      float colwidth = (width - (2 * border))/datalength; 
      float halfcol = colwidth/2; 
      float lasth = 0; 
      for (int i = 0; i < values.length; i++) { 
       float val = values[i] - min; 
       float rat = val/diff; 
       float h = graphheight * rat; 
       if (i > 0) 
        canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint); 
       lasth = h; 
      } 
     } 
    } 
} 

private float getMax() { 
    float largest = Integer.MIN_VALUE; 
    for (int i = 0; i < values.length; i++) 
     if (values[i] > largest) 
      largest = values[i]; 
    return largest; 
} 

private float getMin() { 
    float smallest = Integer.MAX_VALUE; 
    for (int i = 0; i < values.length; i++) 
     if (values[i] < smallest) 
      smallest = values[i]; 
    return smallest; 
} 

,當我打電話 的setContentView(graphView); 它的工作很好,但當我打電話

  diagramRelativeLayout = (RelativeLayout)switcher.getChildAt(i).findViewById(R.id.diagramRelativeLayout); 
      diagramRelativeLayout.addView(graphView); 

這是行不通的。

Error:07-12 11:07:38.933: ERROR/AndroidRuntime(30488): java.lang.RuntimeException: Unable to resume activity {com.meters.src/com.meters.src.Main_Activity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

RelativeLayout包含在ViewFlipper中。我可以如何將視圖添加到此佈局?

回答

0

我做到了,我只是做了一個佈局文件,並在添加對它的引用diagramRelativeLayout

我的XML :

<RelativeLayout 
    android:id = "@+id/diagramRelativeLayout" 
    android:background="@layout/button_style" 
    android:layout_width="fill_parent" 
    android:layout_height = "fill_parent" 
    android:layout_marginTop = "10sp" 
    android:layout_marginLeft = "5sp" 
    android:layout_marginRight = "5sp" 
    android:layout_marginBottom = "10sp" 
    android:gravity="center"> 
    <include android:id="@+id/graph" layout="@layout/graph"/> 
    </RelativeLayout> 

我的Java:

graphLayout = (RelativeLayout)switcher.getChildAt(i).findViewById(R.id.graph); 
graphLayout.addView(graphView); 
2

您沒有添加使用代碼視圖,你可以做到這一點的XML就像任何Android設備上查看:在你的情況 只需添加以下代碼在XML,你wan't你的觀點

<com.your.package.GraphView android:id="@+id/myGraphView" /> 

然後在你的活動中,你使用

GraphView myGV = (GraphView) findViewById(R.id.myGraphView); 
+0

07-12 15:15:59.133:ERROR/AndroidRuntime(31020):了java.lang.RuntimeException:無法恢復活動{com.meters.src/com.meters.src.Main_Activity}:機器人.view.InflateException:二進制XML文件行#136:錯誤膨脹類com.meters.src.GraphView –

0

視圖只能有一個單親可以找回。您正在添加的視圖(我正在猜測重新使用即graphView)已經是另一個視圖層次結構的一部分。如果你真的想重用它(我建議你可能不用),那麼你必須在現有的視圖層次結構中將它從父項中分離出來。

+0

但如何做到這一點 –

+0

檢查出你已經添加此graphView在其他地方比diagramRelativeLayout.addView(graphView); –

+0

我只在oncreate()'graphView = new GraphView(context,values,「Weekly Usage(kW)」,horlabels,verlabels,GraphView.BAR,Color.RED)中初始化它;' –

0

添加像如下代碼行,

diagramRelativeLayout = (RelativeLayout)switcher.getChildAt(i).findViewById(R.id.diagramRelativeLayout); 

diagramRelativeLayout.removeAllViewsInLayout(); // <-- Add this and try 

diagramRelativeLayout.addView(graphView); 
+0

謝謝,但它沒有幫助 –

相關問題