gpt4 book ai didi

java - 如何以编程方式设置列以在 Android 中具有动态?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:07:34 25 4
gpt4 key购买 nike

我创建了一个这样的表:

TableLayout table = new TableLayout(getApplicationContext());
table.setColumnStretchable(1, true);
table.setColumnStretchable(2, true);

tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.LEFT);
for (int j = 0; j < 4; j++) {
statistics = new TextView[4];
statistics[i] = new TextView(getApplicationContext());
statistics[i].setText("Text");
statistics[i].setGravity(Gravity.LEFT);
tableRow[i].addView(statistics[i]);
}
table.addView(tableRow[i]);

这段代码的结果是:

enter image description here

我想实现这个:

enter image description here

这怎么可能?

最佳答案

有了这个

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"

<TableRow
android:layout_weight="1"
android:gravity="center">

<ImageButton
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:scaleType="center"
android:src="@drawable/ic_1"
android:background="@null" />

<ImageButton
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:scaleType="center"
android:src="@drawable/ic_2"
android:background="@null"/>

<ImageButton
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:scaleType="center"
android:src="@drawable/ic_3"
android:background="@null" />
</TableRow>
</TableLayout>

三个按钮对齐。我认为您的代码中需要 gravity="center"和/或 android:stretchColumns="*"。

更新

试试这个:

        TableLayout table = new TableLayout(getApplicationContext());
table.setColumnStretchable(1, true);
table.setColumnStretchable(2, true);
table.setStretchAllColumns(true);

tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.CENTER);
for (int j = 0; j < 4; j++) {
statistics = new TextView[4];
statistics[i] = new TextView(getApplicationContext());
statistics[i].setText("Text");
statistics[i].setGravity(Gravity.LEFT);
tableRow[i].addView(statistics[i]);
}
table.addView(tableRow[i]);

更新

我模拟了你的问题并使用了它

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


TableLayout table = new TableLayout(getApplicationContext());
table.setStretchAllColumns(true);

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

TableRow[] tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.CENTER);

TextView pos = new TextView(getApplicationContext());
pos.setGravity(Gravity.LEFT);
pos.setText(String.valueOf(i) + ". " + getName(i));

TextView a = new TextView(getApplicationContext());
a.setGravity(Gravity.LEFT);
a.setText("2/9");

TextView points = new TextView(getApplicationContext());
points.setGravity(Gravity.LEFT);
points.setText("2/9");

tableRow[i].addView(pos);
tableRow[i].addView(a);
tableRow[i].addView(points);

table.addView(tableRow[i]);
}

RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
container.addView(table);
}

private String getName(int i) {

if (i == 2) {
return "Recooooooooooooooord";
} else if (i == 3) {
return "Recooooooord";
}

return "Fran";
}

结果如你所愿,可以看到结果here

关于java - 如何以编程方式设置列以在 Android 中具有动态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959339/

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