android - TableLayout如何在ImageView之间添加间距

标签 android android-studio tablelayout

我想实现如下效果:

enter image description here

不幸的是,这就是我现在所做的:

enter image description here

如何在 ImageView 之间添加空格以获得与第一张图片相同的效果?这是我的代码:

public class MainActivity extends AppCompatActivity {

    private int totalValue = 2000;
    private int currentValue = 180;
    private int rowsNumber = 10;
    private int columnsNumber = 10;

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

        TableLayout tableLayout = findViewById(R.id.tableLayout);


        for (int i = 0; i < 10; i++) {

            TableRow tableRow = new TableRow(this);
            tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));

            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) {

        return R.drawable.test;
    }
}

最佳答案

使用 TableRow.LayoutParams lp = new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 它将起作用。

            TableRow.LayoutParams lp = new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = margin;
            lp.topMargin = margin;
            lp.rightMargin = margin;
            lp.bottomMargin = margin;
            imageView.setLayoutParams(lp);

或者你可以使用imageView.setPadding(padding, padding, padding, padding);

干杯。

关于android - TableLayout如何在ImageView之间添加间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59357754/

相关文章:

android - 无法继承和覆盖 ArrayAdapter

android - 不允许 TextView 填充整个列

Android M反射方法freeStorageAndNotify异常

java - Android ImageView 克服线性布局

android - 检查 fragment 是否显示为对话框以更改点击监听器行为

Android 保存文件权限被拒绝

android - Gradle 脚本自动版本并在 Android 中包含提交哈希

android - 为什么我们没有一个文件来打开 Android Studio 项目?

java - Android TableLayout 内容不显示

javascript - 最简单的表格条目编辑/添加/删除 Web 工具包(与 php 一起使用)