2014-09-29 76 views
1

我已經使用GraphView API製作了兩張圖,並將它們分別放在兩個LinearLayout之內。這兩種佈局都在ScrollView之內,它位於Relativelayout之內。出於某種原因,該ScrollView不會滾動:我的ScrollView將不會滾動

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.adrian.desktop.ViewProgress$PlaceholderFragment"> 
    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/scrollView" 
     android:fillViewport="true"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="280dp" 
      android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/Graph" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 
     </LinearLayout> 
      <LinearLayout 
       android:id="@+id/Graph2" 
       android:layout_below="@+id/Graph" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1"> 
      </LinearLayout> 
     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

的Java:

package com.example.adrian.desktop; 

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout;  
import com.jjoe64.graphview.GraphView; 
import com.jjoe64.graphview.GraphView.GraphViewData; 
import com.jjoe64.graphview.GraphViewSeries; 
import com.jjoe64.graphview.LineGraphView; 

public class ViewProgress extends FragmentActivity {  
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_view_progress); 
     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()) 
       .commit(); 
     } 
     GraphView graphView = new LineGraphView(this, "example"); 
     // first init data 
     // sin curve 
     int num = 150; 
     GraphViewData[] data = new GraphViewData[num]; 
     double v=0; 
     for (int i=0; i<num; i++) { 
      v += 0.2; 
      data[i] = new GraphViewData(i, Math.sin(v)); 
     } 
     GraphViewSeries seriesSin = new GraphViewSeries("This Year", new GraphViewSeries.GraphViewSeriesStyle(Color.rgb(200, 50, 00), 3), data); 
     // cos curve 
     data = new GraphViewData[num]; 
     v = 0; 
     for (int i=0; i<num; i++) { 
      v += 0.2; 
      data[i] = new GraphViewData(i, Math.cos(v)); 
     } 
     GraphViewSeries seriesCos = new GraphViewSeries("Last Year", new GraphViewSeries.GraphViewSeriesStyle(Color.rgb(90, 250, 00), 3), data); 
     /* 
     * create graph 
     */ 
     graphView = new LineGraphView(this, "Grades"); 
     // add data 
     graphView.addSeries(seriesCos); 
     graphView.addSeries(seriesSin); 
     // optional - set view port, start=2, size=10 
     graphView.setViewPort(2, 10); 
     graphView.setScalable(true); 
     // optional - legend 
     graphView.setShowLegend(true); 
     graphView.setLegendAlign(GraphView.LegendAlign.BOTTOM); 
     graphView.setHorizontalLabels(new String[] {"Last Year", "First Term", "Second Term", "Third Term"}); 
     graphView.setVerticalLabels(new String[] {"A*", "A", "B","C","D","E","F"}); 
     LinearLayout layout = (LinearLayout) findViewById(R.id.Graph); 
     LinearLayout layout2 = (LinearLayout)findViewById(R.id.Graph2); 
     layout.addView(graphView); 
     // init example series data 
     GraphViewSeries exampleSeries = new GraphViewSeries(new GraphViewData[] { 
      new GraphViewData(1, 2.0d) 
       , new GraphViewData(2, 1.5d) 
       , new GraphViewData(3, 2.5d) 
       , new GraphViewData(4, 1.0d) 
     }); 
     GraphView graphView2 = new LineGraphView(
      this // context 
      , "Performance" // heading 
     ); 
     graphView2.addSeries(exampleSeries); // data  
     layout2.addView(graphView2); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.view_progress, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment {  
     public PlaceholderFragment() { 
     }  
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_view_progress, container, false); 
      return rootView; 
     } 
    } 
} 
+0

滾動型只能有1直接孩子。 – axierjhtjz 2014-09-29 10:39:17

回答

0

做了滾動的根視圖。刪除它是多餘的相對佈局


+0

至少在投票時發表評論,所以我可以知道原因-_- – Elgert 2014-09-29 10:28:40

0

嘗試以下xml。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.adrian.desktop.ViewProgress$PlaceholderFragment"> 


    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/scrollView" 
     android:fillViewport="true"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="280dp" 
      android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/Graph" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
     </LinearLayout> 
     <LinearLayout 
      android:id="@+id/Graph2" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      </LinearLayout> 
     </LinearLayout> 
      </ScrollView> 
</RelativeLayout> 
0

你提供layout_weight = 1,兩個孩子的線性佈局,而不是爲每個孩子的家長線性Layout.so高度設定Weightsum將等於它的父母身高/ 2。簡而言之,滾動視圖高度=全屏幕,每個孩子將有一半的屏幕高度。這意味着不需要滾動。試試這個:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ScrollView 
    android:id="@+id/scrollView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="280dp" 
     android:background="#000000" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/Graph" 
      android:layout_width="match_parent" 
      android:layout_height="500dp" 

      android:background="#ff0000" 
      android:orientation="vertical" > 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/Graph2" 
      android:layout_width="match_parent" 
      android:layout_height="1000dp" 

      android:background="#00ff00" 
      android:orientation="vertical" > 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

+0

我知道我不適合在屏幕上,但我想展示滾動功能,這就是爲什麼我特別想要滾動;請你能演示一個允許滾動的版本 – 2014-09-29 11:27:32

+0

請你能詳細說明一下嗎?我不知道你在說什麼。請詳細解釋。 – 2014-09-29 11:53:13

+0

如果您的所有子視圖的總高度將大於您的屏幕大小,那麼您將能夠滾動項目。 – 2014-09-29 11:57:08