首先我们明确一点,高亮值是出现在一个TextView中的
因此我们首先应该设置他的布局,放置一个TextView:(正常操作)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/contentview"
android:layout_centerHorizontal="true"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="7dp"
android:ellipsize="end"
android:singleLine="true"
android:textSize="15sp"
android:layout_width="wrap_content"
android:textColor="#000000"
android:layout_height="wrap_content" />
</RelativeLayout>
新建一个类继承Markerview
关于此类,源码里是这样介绍的:在图表中选择值时可以显示的视图。扩展这个类,为您的markers提供custcm布局。
我们可以理解为就是扩展布局的意思:
public class Marker extends MarkerView {
private TextView textView;
private IAxisValueFormatter iAxisValueFormatter;
private DecimalFormat format;//数字格式化
public Marker(Context context,IAxisValueFormatter iAxisValueFormatter) {
super(context,R.layout.marker_layout);
this.iAxisValueFormatter=iAxisValueFormatter;
textView=(TextView)findViewById(R.id.contentview);
format=new DecimalFormat("###.0");//定义数字格式
}
@Override
//进行重绘
public void refreshContent(Entry e, Highlight highlight) {
textView.setText("x:"+iAxisValueFormatter.getFormattedValue(e.getX(),null)+",y:"+format.format(e.getY()));
super.refreshContent(e, highlight);
}
@Override
public MPPointF getOffset() {
return new MPPointF(-(getWidth()/2),-getHeight());
}
}
接下来在需要用到高亮的地方设置它:
那lineChart打个比方:
Marker marker=new Marker(this, new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return zuobiao[(int) value];
}
});
marker.setChartView(lineChart);
lineChart.setMarker(marker);
这里的zuobaio 是我的放置x数据是数据
这样我们的高亮就算是设置成功了,点击折现图的点,对应会出现它的位置信息