我想在一个片段中显示一个图形,但是我没有找到很多关于如何使用 GraphView 的信息。有人可以帮我显示一个简单的图表吗?
我从这个开始:http ://android-graphview.org/#doc_createsimplegraph 但不要让它在片段中工作。
我想在一个片段中显示一个图形,但是我没有找到很多关于如何使用 GraphView 的信息。有人可以帮我显示一个简单的图表吗?
我从这个开始:http ://android-graphview.org/#doc_createsimplegraph 但不要让它在片段中工作。
我所做的是创建一个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。
在 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);
仅作记录:您不能将它与 ScrollViews 结合使用。如果 ScrollView 正在包装您的 LinearLayout,则 lib 不会显示任何内容。