我的ScrollView不会滚动

时间:2014-09-29 10:04:58

标签: java android xml android-graphview

我使用GraphView API创建了两个图表,并将它们分别放在两个LinearLayout内。两个布局都位于ScrollViewRelativelayout内。出于某种原因,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>

爪哇:

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

3 个答案:

答案 0 :(得分:0)

将ScrollView作为根视图。删除它是多余的相对布局


答案 1 :(得分: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>

答案 2 :(得分:0)

您为子线性布局提供了layout_weight = 1,而没有为父线性布局设置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>