在TextView背景上绘制条形图

Draw bar chart on TextView background

我有一个 ListView,其中包含一个 TextView 作为列表项中的一项。自定义适配器扩展 ArrayAdapter。 TextView 将包含名称。我还想在 TextView 背景中的文本下绘制一个简单的条形图。它将代表每个销售人员的月销售额。因此它将有 12 个垂直条(每个月一个)按比例跨越 TextView 的宽度(每个条是 TextView 宽度的十二分之一)。

执行此操作的正确方法是什么?我对 Java 和 Android 比较陌生;但是在其他平台上有多年的编程经验,所以你知道我希望获得的详细程度。例如。我将在哪里实现绘图代码(在适配器的 getView() 中?)以及如何在 TextView 的背景上绘制简单的条形图?谢谢。

您有几个选项,您可以在 canvas 上绘制自定义条形图。然后将您的文本视图放在它上面。

https://developer.android.com/training/custom-views/custom-drawing

或者您可以使用 MpAndroidChart 之类的东西为您制作条形图,然后再次将您的文本视图放在上面。

https://github.com/PhilJay/MPAndroidChart

更新:

查看此 codelab 自定义绘图。

我建议在功能更强大的列表 RecyclerView 中执行类似的操作,您可以在将视图绑定到回收器视图时填充数据。这将为您提供更多的运行时灵活性和效率来处理此自定义场景。

您可以使用几种不同的布局类型堆叠视图,但重叠视图在 FrameLayout

中最简单
<Whatever Your List Item Parent Layout Is

   <FrameLayout
          android:id="@+id/myListFram"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

     <ImageView
            android:id="@+id/my_image_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

     <TextView
            android:id="@+id/my_text_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

   </FrameLayout>
</ Close Parent>

看看这个: https://academy.realm.io/posts/360-andev-2017-joshua-lamson-custom-drawing-canvas/