在android中的Graphview中更改文本颜色

问题描述 投票:0回答:1

这是GraphView.enter image description here的图片如您所见,文本是黑色。我希望将这种颜色改为白色。我怎样才能改变这些文本?

public class MainActivity extends AppCompatActivity {

GraphView graph;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    graph = (GraphView) findViewById(R.id.graph);

    StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);
    staticLabelsFormatter.setHorizontalLabels(new String[] {"Yan", "Fev", "Mart", "Apr", "May", "Iyn",
    "Iyl", "Avq", "Sent", "Okt", "Noy", "Dek"});
    //staticLabelsFormatter.setVerticalLabels(new String[] {"low", "middle", "high", "koko"});
    graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);

    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
            new DataPoint(0, 2),
            new DataPoint(1,5),
            new DataPoint(2, 4),
            new DataPoint(3, 4),
            new DataPoint(4, 8),
            new DataPoint(5, 6),
            new DataPoint(6, 8),
            new DataPoint(7, 1),
            new DataPoint(8, 5),
            new DataPoint(9, 2),
            new DataPoint(10, 7),
            new DataPoint(11, 3),

    });
    series.setColor(Color.YELLOW);
    series.setDrawBackground(true);
    series.setDrawDataPoints(true);
    graph.addSeries(series);

}
android textcolor android-graphview
1个回答
0
投票

不幸的是,由于缺少GraphView方法的文档,我也遇到了这个问题,我很快就转向了MPAndroidChart,但我想再尝试在GraphView中更改标签颜色。因此,以下是我发现的解决方案:

graph.getGridLabelRenderer().setVerticalLabelsColor(Color.WHITE);
graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.WHITE);
graph.getGridLabelRenderer().setVerticalLabelsColor(Color.WHITE);
graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.WHITE);
graph.getGridLabelRenderer().reloadStyles();

因此将这些行与提供的代码合并:

public class MainActivity extends AppCompatActivity {

GraphView graph;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    graph = (GraphView) findViewById(R.id.graph);

    StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);
    staticLabelsFormatter.setHorizontalLabels(new String[] {"Yan", "Fev", "Mart", "Apr", "May", "Iyn",
    "Iyl", "Avq", "Sent", "Okt", "Noy", "Dek"});
    //staticLabelsFormatter.setVerticalLabels(new String[] {"low", "middle", "high", "koko"});
    graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);

    //First two lines change the grid line color
    graph.getGridLabelRenderer().setGridColor(Color.WHITE);
    graph.getGridLabelRenderer().setHighlightZeroLines(false);

    //Below two lines change the label color
    graph.getGridLabelRenderer().setVerticalLabelsColor(Color.WHITE);
    graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.WHITE);
    graph.getGridLabelRenderer().reloadStyles();

    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
            new DataPoint(0, 2),
            new DataPoint(1,5),
            new DataPoint(2, 4),
            new DataPoint(3, 4),
            new DataPoint(4, 8),
            new DataPoint(5, 6),
            new DataPoint(6, 8),
            new DataPoint(7, 1),
            new DataPoint(8, 5),
            new DataPoint(9, 2),
            new DataPoint(10, 7),
            new DataPoint(11, 3),

    });
    series.setColor(Color.YELLOW);
    series.setDrawBackground(true);
    series.setDrawDataPoints(true);
    graph.addSeries(series);

}
© www.soinside.com 2019 - 2024. All rights reserved.