gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:37:31 26 4
gpt4 key购买 nike

我想实现如下效果:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com