1

我想在一个片段中显示一个图形,但是我没有找到很多关于如何使用 GraphView 的信息。有人可以帮我显示一个简单的图表吗?

我从这个开始:http ://android-graphview.org/#doc_createsimplegraph 但不要让它在片段中工作。

4

3 回答 3

2

我所做的是创建一个fragment_graph.xml

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

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="250dip"
    android:id="@+id/graph1"
    android:background="#102f65" />
</LinearLayout>

并在 Fragment 类中添加相关代码:

public class GraphFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, 
                           ViewGroup container, 
                           Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_graph, container, false);

    // add the data here if necessary
    // ...

    LinearLayout layout = (LinearLayout) view.findViewById(R.id.graph1);
    layout.addView(graphView);

    return view;
  }
  // ...
}

然后您可以“按原样”使用片段。

大多数功劳实际上归功于vogella

于 2014-02-21T21:31:59.753 回答
0

在 layout.xml 文件中定义一个具有固定宽度和高度的空 LinearLayout,然后将 graphview 视图添加到其中。

布局

<LinearLayout
                    android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="250dip"
                        android:id="@+id/graph1" />

爪哇

LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
于 2013-11-17T20:02:50.163 回答
0

仅作记录:您不能将它与 ScrollViews 结合使用。如果 ScrollView 正在包装您的 LinearLayout,则 lib 不会显示任何内容。

于 2014-03-19T20:37:06.023 回答