gpt4 book ai didi

java - Android 应用程序,如何以编程方式在 TableLayout 单元格中居中对齐 ImageView

转载 作者:行者123 更新时间:2023-11-29 17:00:14 25 4
gpt4 key购买 nike

我在 Android 应用程序中有以下代码:

TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,5,0,0);
row.setLayoutParams(lp);
List<List<String>> cList2 = displayTemp.getCList2();
StringBuffer sb = new StringBuffer();
List<String> lStr = cList2.get(0);
for (int ix = 0; ix < lStr.size(); ix++) {
sb.append(lStr.get(ix));
if (ix != lStr.size() - 1) {
sb.append("\n");
}
}
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
tv.setText(sb.toString());
tv.setTextSize(COMPLEX_UNIT_DIP, 20);
row.addView(tv);
byte[] decodedString = Base64.decode(cList2.get(1).get(0), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
ImageView iView = new ImageView(this);
iView.setImageBitmap(decodedByte);
row.addView(iView);
tableLayout2.addView(row, rowNum);
rowNum++;

这会动态显示表格行。我的第二个表格单元格显示有这段代码,

byte[] decodedString = Base64.decode(cList2.get(1).get(0), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
ImageView iView = new ImageView(this);
iView.setImageBitmap(decodedByte);
row.addView(iView);

第二个单元格中显示的图像当前是右对齐的。我想将其显示为在单元格中居中对齐。我该怎么做?

最佳答案

添加下面一行代码

lp.gravity = Gravity.CENTER;

最终代码

TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
//rest of code
lp.gravity = Gravity.CENTER;
row.setLayoutParams(lp);

Gravity有以下选项。

Gravity.CENTER_HORIZONTAL
重力.CENTER_VERTICAL

关于java - Android 应用程序,如何以编程方式在 TableLayout 单元格中居中对齐 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43337641/

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