我已經使用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;
}
}
}
滾動型只能有1直接孩子。 – axierjhtjz 2014-09-29 10:39:17