TableLayout如何在ImageView之间添加间距

问题描述 投票:1回答:1

我想实现以下效果:

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;
    }
}
android android-studio tablelayout
1个回答
0
投票
    ImageView imageView = new ImageView(this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    //Enter your margin below:
    lp.setMargins(left, top, right, bottom);

    imageView.setLayoutParams(lp);
    imageView.setImageResource(this.getCellDrawableId(i, j, totalValue, currentValue));
    tableRow.addView(imageView, j);

尝试一下,让我知道是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.