假设我的总值是2000,而我的当前值是180。我想把它图形化地呈现如下:

我怎么能这么做呢?我正在寻找许多图表,但我在任何地方都没有找到这样的解决方案。有没有开放源码提供这种图形化的值表示?
发布于 2019-12-13 22:54:52
您可以使用TableLayout来实现这一点。您的布局将如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableLayout>然后,在您的代码中,您可以像这样填充表:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int totalValue = 2000;
int currentValue = 180;
int rowsNumber = 10;
int columnsNumber = 10;
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
for (int i = 0; i < rowsNumber ; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tableRow.setBackgroundResource(R.drawable.yourRowDrawable);
for (int j = 0; j < columnsNumber; j++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(this.getCellDrawableId(i,j,totalValue ,currentValue ));
tableRow.addView(imageView, j);
}
tableLayout.addView(tableRow, i);
}
}
}
private int getCellDrawableId(int i, int j,int totalValue ,int currentValue ){
if(/*some logic here*/)
return R.drawable.greenCell;
return R.drawable.emptyGrayCell;
}P.S.:其中R.drawable.greenCell,R.drawable.emptyGrayCell,R.drawable.yourRowDrawable是适当的可绘制来表示你的网格。
另外,我没有测试这段代码,只是像代码片段一样写在这里,所以这里可能会出现一些错误
https://stackoverflow.com/questions/59323398
复制相似问题