我正在尝试使用Android GraphView库(http://www.android-graphview.org/),但是无论我如何尝试,似乎都无法显示水平轴标题和标签。 Y轴和标题似乎没有任何问题。
基于之前的S.O.回答“ GraphView, how to show x-axis label?”,它应该很简单
GridLabelRenderer gridLabel = graphView.getGridLabelRenderer();
gridLabel.setHorizontalAxisTitle("X Axis Title");
但是我已经尝试过了,但对我也不起作用。
我的代码如下:
fragment_results.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResultsFragment">
<com.jjoe64.graphview.GraphView
android:id="@+id/results_graph"
android:layout_height="400dp"
android:layout_width="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:visibility="visible"/>
</android.support.constraint.ConstraintLayout>
ResultsFragment.java中的onCreate()
View v = inflater.inflate(R.layout.fragment_results, container, false);
mGraph = v.findViewById(R.id.results_graph);
mGraph.getViewport().setScalable(true); // enables horizontal zooming and scrolling
mGraph.getViewport().setScalableY(true); // enables vertical zooming and scrolling
mGraph.getViewport().setYAxisBoundsManual(true); // Prevents auto-rescaling the Y-axis
mGraph.getViewport().setXAxisBoundsManual(true); // Prevents auto-rescaling the X-axis
mGraph.setTitleTextSize(96);
mGraph.setTitle("Title");
mGraph.getGridLabelRenderer().setHumanRounding(true);
mGraph.getGridLabelRenderer().setVerticalAxisTitleTextSize(64);
mGraph.getGridLabelRenderer().setVerticalAxisTitle("Y Axis Title");
mGraph.getGridLabelRenderer().setHorizontalAxisTitleTextSize(64);
mGraph.getGridLabelRenderer().setHorizontalAxisTitle("X Axis Title");
mGraph.getGridLabelRenderer().setHorizontalLabelsVisible(true);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
new DataPoint(34.3, -21.0),
new DataPoint(37.0, -21.0),
new DataPoint(37.3, -18.0),
new DataPoint(41.0, -18.0),
new DataPoint(41.3, -15.0),
new DataPoint(46.0, -15.0),
new DataPoint(46.3, -12.0),
new DataPoint(54.0, -12.0),
new DataPoint(54.3, -9.0),
new DataPoint(65.0, -9.0),
new DataPoint(65.3, -6.0),
new DataPoint(84.0, -6.0),
new DataPoint(84.3, -3.0),
new DataPoint(124.0, -3.0),
new DataPoint(124.3, 0.0)
});
series.setTitle("TEST");
series.setColor(Color.rgb(115,230,115));
mGraph.addSeries(series);
结果是这样的:
答案 0 :(得分:0)
我创建了一个新的Android Studio项目,并复制了上面的代码,一切正常。沮丧的是,我采用了蛮力的方法,只是比较每个文件中的代码。我发现的唯一区别是,我的AndroidManifest.xml中的硬件加速功能已关闭
<application
...
android:hardwareAccelerated="false">
...
</application>
从清单中删除该行之后,X轴标题和标签终于出现了!