2013-05-30 170 views
0

我创建条形图我的应用程序,但我的条形图不滚动能,所以我增加了我的内衬设计成横向滚动视图..如何在水平滚动视图中显示扩展视图类的类...?

这里是我的xml文件是...

<?xml version="1.0" encoding="utf-8"?> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/horizaontalforscroll"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/linearforscroll" > 
</LinearLayout> 

这里我打电话延伸View类MyGraph类..

LinearLayout linear=(LinearLayout) findViewById(R.id.linearforscroll); 
    MyGraph graph = new MyGraph(this); 
    linear.addView(graph); 
    HorizontalScrollView hr = (HorizontalScrollView)findViewById(R.id.horizaontalforscroll); 
    hr.removeAllViewsInLayout(); 
    hr.addView(linear); 

添加最后我MyGraph类..

 public class MyGraph extends View{ 

     private static final int SHAPE_WIDTH = 10; 
     private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
     public MyGraph(Context context) { 
      // TODO Auto-generated constructor stub 
      super(context); 
     } 
     @Override 
     protected void onDraw(Canvas canvas){ 
      // TODO Auto-generated method stub 
      super.onDraw(canvas); 
      paint.setStyle(Style.FILL); 
      int legendSize = 0; 
      if(legendSize == 0){ 
       legendSize = height/5; 
      } 
      int[] margine = new int[]{40,30,20,200,250}; 
      int startX = x + margine[0]; 
      int startY = y; 
      int stopX = x + margine[0]; 
      int stopY = 0; 
      Log.d("JWP", "W & H:"+width+":"+height); 
      if(width < height){ 
       stopY = y + height - margine[4]; 
      } 
      else{ 
       stopY = y + height - margine[3]; 
      } 

      int length = values.length; 
      paint.setColor(Color.WHITE); 
      canvas.drawLine(startX, startY, stopX, stopY, paint); 
      canvas.drawLine(stopX, stopY, x + width, stopY, paint); 

      int rHeight = stopY; 
      for(int i = 0;i < length; i++){ 
       double percentage = (values[i]*rHeight)/maxValue; 
       Log.d("JWP", "Values:"+values[i]); 
       Log.d("JWP", "PERCENTAGE:"+percentage); 
       paint.setColor(COLORS[(i) % COLORS.length]); 
       int modifiedStratY = rHeight - (int) percentage + 20; 
       if(modifiedStratY > rHeight){ 
        modifiedStratY = rHeight - (int) percentage; 
       } 
       canvas.drawRect(startX, modifiedStratY, startX+30, rHeight, paint); 
       paint.setTextSize(15); 
       canvas.drawText(String.valueOf(values[i]), startX, modifiedStratY, paint); 
       canvas.save(); 
       canvas.rotate(35, startX, rHeight + 7); 
       paint.setColor(Color.WHITE); 
       paint.setTextSize(13); 
       canvas.drawText(name[i], startX, rHeight + 7, paint); 
       canvas.restore(); 
       startX += 70; 
      } 

      String xTitle = "Directory Name"; 
      int xTitleLength = xTitle.length()/2; 
      int xTitleStart = (x + width/2) - xTitleLength/2; 
      paint.setTextSize(15); 
      canvas.drawText(xTitle, xTitleStart,y + rHeight + 65, paint); 

      String yTitle = "Amount of Space in MB"; 
      int yTitleLength = yTitle.length(); 
      int yTitleStart = (y + rHeight/2) + yTitleLength*2;   
      canvas.save(); 
      canvas.rotate(-90, x + 10 , yTitleStart); 
      canvas.drawText(yTitle, x + 10, yTitleStart, paint); 
      canvas.restore(); 

      paint.setTextSize(12); 
      canvas.drawText(String.valueOf(maxValue), x + 12, y + 20, paint); 
      canvas.drawText("0.0", x + 11, rHeight, paint); 
      float yLabelValue = (float) maxValue/4; 
      int yLabelPoints = rHeight/4; 
      canvas.drawText(String.valueOf(yLabelValue*3), x + 12, (rHeight - yLabelPoints*3 + 20), paint); 
      for(int j = 1; j < 3; j++){ 
       canvas.drawText(String.valueOf(yLabelValue), x + 12, (rHeight - yLabelPoints + 20), paint); 
       yLabelPoints += yLabelPoints; 
       yLabelValue += yLabelValue; 
      } 

      int left = x + 15; 
      int right = x + width - 5; 
      String[] title = new String[length]; 
      for(int i = 0; i < length; i++){ 
       title[i] = name[i]; 
      } 
      drawLegends(canvas,title,left,right,y,width,height,legendSize,paint); 
     } 
} 

我的查询是,当我运行运行的代码没有显示在屏幕上,如果我跑没有水平滚动视图的结果清楚地显示.. 请帮我解决这个问题..

谢谢。

+0

你应该给'LinearLayout'里面的'Horizo​​ntalScrollView'的方向。例如'android:orientation =“horizo​​ntal”'。为什么你要包含这一行:'hr.removeAllViewsInLayout();'? – Ole

+0

我这样做,是因为你告诉,但是当我再次运行屏幕是空白的。当我运行控制是不是去那类的内部 –

+0

意思..为什么它是......? –

回答

0

变化

LinearLayout linear=(LinearLayout) findViewById(R.id.linearforscroll); 
    MyGraph graph = new MyGraph(this); 
    linear.addView(graph); 
    HorizontalScrollView hr = (HorizontalScrollView)findViewById(R.id.horizaontalforscroll); 
    hr.removeAllViewsInLayout(); 
    hr.addView(linear); 

到:

LinearLayout linear=(LinearLayout) findViewById(R.id.linearforscroll); 
    linear.removeAllViewsInLayout(); 
    MyGraph graph = new MyGraph(this); 
    linear.addView(graph); 
+0

多德它不工作,当我运行它说明不了什么我都试过.. –

+0

您可以尝试的EditText(你好世界),而不是LinearLayout中丢弃问题Horizo​​ntalScrollView – mromer

+0

好哥们..谢谢你。 –