如何在 java 代码中为 android 实现滚动视图?
How to implement scroll view in java code for android?
我有这个 java 代码,可以在 table 中的绘图上创建按钮。我试图让整个 table 能够垂直滚动,但我尝试过的每一种方式都会给我一个错误。当我取消注释我的 2 行滚动视图时,我的应用程序崩溃了。任何帮助将不胜感激。
public void drawAllGames(UserData u){
int nGames = u.lastGame;
TableLayout table = (TableLayout) findViewById(R.id.TableForButtons);
//ScrollView scroll = new ScrollView(this);
//((ScrollView) scroll).addView(table);
if(nGames != 0) {
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
TextView title1 = new TextView(this);
title1.setText("Awaiting you");
tableRow.addView(title1);
//forloops fills games awaiting my turn
for (int i = 0; i < nGames; i++) {
if (u.isMyTurn(u.allGames[i].gameid)) {
tableRow = new TableRow(this);
table.addView(tableRow);
Button gameButton = new Button(this);
gameButton.setText("Play");
tableRow.addView(gameButton);
TextView label = new TextView(this);
String partner;
if (u.allGames[i].partner_A == u.getName())
partner = u.allGames[i].partner_B;
else
partner = u.allGames[i].partner_A;
label.setText(" " + partner + " Round: " + u.allGames[i].getRound());
tableRow.addView(label);
final int game = i;
gameButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
resumeGame(game);
}
}
);
}
}
tableRow = new TableRow(this);
table.addView(tableRow);
TextView title2 = new TextView(this);
title2.setText("Waiting for partner");
tableRow.addView(title2);
//forloops fills games awaiting my turn
for (int i = 0; i < nGames; i++) {
if (!u.isMyTurn(u.allGames[i].gameid)) {
tableRow = new TableRow(this);
table.addView(tableRow);
TextView label = new TextView(this);
String partner;
if (u.allGames[i].partner_A == u.getName())
partner = u.allGames[i].partner_B;
else
partner = u.allGames[i].partner_A;
label.setText(" " + partner + " Round: " + u.allGames[i].getRound());
tableRow.addView(label);
final int game = i;
}
}
}
else{
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
TextView title3 = new TextView(this);
title3.setText("You have no games, start a new one now!");
tableRow.addView(title3);
}
}
您必须在布局中使用 ScrollView。
这是ScrollView的使用:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<!-- everything you already have -->
</TableLayout>
</ScrollView>
我有这个 java 代码,可以在 table 中的绘图上创建按钮。我试图让整个 table 能够垂直滚动,但我尝试过的每一种方式都会给我一个错误。当我取消注释我的 2 行滚动视图时,我的应用程序崩溃了。任何帮助将不胜感激。
public void drawAllGames(UserData u){
int nGames = u.lastGame;
TableLayout table = (TableLayout) findViewById(R.id.TableForButtons);
//ScrollView scroll = new ScrollView(this);
//((ScrollView) scroll).addView(table);
if(nGames != 0) {
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
TextView title1 = new TextView(this);
title1.setText("Awaiting you");
tableRow.addView(title1);
//forloops fills games awaiting my turn
for (int i = 0; i < nGames; i++) {
if (u.isMyTurn(u.allGames[i].gameid)) {
tableRow = new TableRow(this);
table.addView(tableRow);
Button gameButton = new Button(this);
gameButton.setText("Play");
tableRow.addView(gameButton);
TextView label = new TextView(this);
String partner;
if (u.allGames[i].partner_A == u.getName())
partner = u.allGames[i].partner_B;
else
partner = u.allGames[i].partner_A;
label.setText(" " + partner + " Round: " + u.allGames[i].getRound());
tableRow.addView(label);
final int game = i;
gameButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
resumeGame(game);
}
}
);
}
}
tableRow = new TableRow(this);
table.addView(tableRow);
TextView title2 = new TextView(this);
title2.setText("Waiting for partner");
tableRow.addView(title2);
//forloops fills games awaiting my turn
for (int i = 0; i < nGames; i++) {
if (!u.isMyTurn(u.allGames[i].gameid)) {
tableRow = new TableRow(this);
table.addView(tableRow);
TextView label = new TextView(this);
String partner;
if (u.allGames[i].partner_A == u.getName())
partner = u.allGames[i].partner_B;
else
partner = u.allGames[i].partner_A;
label.setText(" " + partner + " Round: " + u.allGames[i].getRound());
tableRow.addView(label);
final int game = i;
}
}
}
else{
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
TextView title3 = new TextView(this);
title3.setText("You have no games, start a new one now!");
tableRow.addView(title3);
}
}
您必须在布局中使用 ScrollView。 这是ScrollView的使用:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<!-- everything you already have -->
</TableLayout>
</ScrollView>