当前位置: 首页 > 工具软件 > SmartTable > 使用案例 >

SmartTable展示数组数据

程俊誉
2023-12-01
public class MainActivity extends AppCompatActivity {

    private SmartTable<String> table;

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

        table = findViewById(R.id.table);

        String[] week = {"日", "一", "二", "三", "四", "五", "六"};

        String[][] strings = {{"日", "一", "二", "三", "四", "五", "六"}, {"日", "一", "二", "三", "四", "五", "六"},
                {"日", "一", "二", "三", "四", "五", "六"}};

        ArrayTableData<String> data = ArrayTableData.create("日程表", week,
                ArrayTableData.transformColumnArray(strings),
                new IDrawFormat<String>() {
                    @Override
                    public int measureWidth(Column<String> column, int position, TableConfig config) {
                        return 100;
                    }

                    @Override
                    public int measureHeight(Column<String> column, int position, TableConfig config) {
                        return 100;
                    }

                    @Override
                    public void draw(Canvas c, Rect rect, CellInfo<String> cellInfo, TableConfig config) {
                        Paint paint = config.getPaint();
                        paint.setTypeface(Typeface.DEFAULT_BOLD);
                        paint.setStyle(Paint.Style.FILL);
                        paint.setColor(Color.parseColor("#000000"));
                        c.drawText(cellInfo.value, rect.centerX(), rect.centerY()+10, paint);
                    }
                });

        table.getConfig().setColumnTitleBackground(new IBackgroundFormat() {
            @Override
            public void drawBackground(Canvas canvas, Rect rect, Paint paint) {
                canvas.drawColor(Color.parseColor("#999999"));
            }
        });
        table.getConfig().setContentBackground(new IBackgroundFormat() {
            @Override
            public void drawBackground(Canvas canvas, Rect rect, Paint paint) {
                canvas.drawColor(Color.parseColor("#ffffff"));
            }
        });

        table.setTableData(data);
    }
}
 类似资料: