Android 可编程滚动的TableLayout
我在我的Android 可编程滚动的TableLayout,android,android-layout,android-studio,Android,Android Layout,Android Studio,我在我的表格布局中放置了60个按钮,这些按钮超出了屏幕,因此我需要一个滚动条来显示剩余的按钮。我已尝试此代码,但应用程序不工作 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TableLayout tableLayout; tableLay
表格布局中放置了60个按钮,这些按钮超出了屏幕,因此我需要一个滚动条来显示剩余的按钮。我已尝试此代码,但应用程序不工作
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout tableLayout;
tableLayout = (TableLayout) findViewById(R.id.table_layout);
TableRow tr;
int buttonNumber = 0;
for (int i =0;i<30;i++)
{
tr = new TableRow(this);
for (int j =0;j<2;j++)
{
buttonNumber++;
final Button btn = new Button(this);
btn.setText("Button" + (buttonNumber));
btn.setTag(buttonNumber);
btn.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.MATCH_PARENT,1));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
tr.addView(btn);
}
tableLayout.addView(tr);
}
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
super.setContentView(sv);
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
表布局表布局;
tableLayout=(tableLayout)findviewbyd(R.id.table\u布局);
TableRow tr;
int buttonNumber=0;
对于(int i=0;i有几种解决方案,主要问题是您多次调用setContentView()
,一次调用super.
最好的方法是删除ScrollView代码:
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
super.setContentView(sv);
并将其直接添加到布局文件R.layout.activity\u main
,这应该可以做到。有几种解决方案,主要问题是您多次调用setContentView()
,一次调用super.
// try this way,hope this will help you...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tableLayout = new TableLayout(this);
int buttonNumber = 0;
for (int i =0;i<30;i++)
{
TableRow tr = new TableRow(this);
for (int j =0;j<2;j++)
{
buttonNumber++;
final Button btn = new Button(this);
btn.setText("Button" + (buttonNumber));
btn.setTag(buttonNumber);
btn.setLayoutParams(new TableRow.LayoutParams(0,TableRow.LayoutParams.MATCH_PARENT,1f));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,String.valueOf((Integer)view.getTag()),Toast.LENGTH_SHORT).show();
}
});
tr.addView(btn);
}
tableLayout.addView(tr);
}
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
setContentView(sv);
}
![enter image description here][1]
[1]: http://i.stack.imgur.com/3qLk1.png
最好的方法是删除ScrollView代码:
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
super.setContentView(sv);
并将其直接添加到布局文件R.layout.activity\u main
,应该可以做到这一点。//试试这种方法,希望对您有所帮助。。。
// try this way,hope this will help you...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tableLayout = new TableLayout(this);
int buttonNumber = 0;
for (int i =0;i<30;i++)
{
TableRow tr = new TableRow(this);
for (int j =0;j<2;j++)
{
buttonNumber++;
final Button btn = new Button(this);
btn.setText("Button" + (buttonNumber));
btn.setTag(buttonNumber);
btn.setLayoutParams(new TableRow.LayoutParams(0,TableRow.LayoutParams.MATCH_PARENT,1f));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,String.valueOf((Integer)view.getTag()),Toast.LENGTH_SHORT).show();
}
});
tr.addView(btn);
}
tableLayout.addView(tr);
}
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
setContentView(sv);
}
![enter image description here][1]
[1]: http://i.stack.imgur.com/3qLk1.png
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TableLayout TableLayout=新的TableLayout(本);
int buttonNumber=0;
对于(int i=0;i//尝试这种方法,希望这将帮助您。。。
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TableLayout TableLayout=新的TableLayout(本);
int buttonNumber=0;
对于(int i=0;我可以添加R.layout.activity\u main的xml代码吗?这样我就可以看到其中是否有bug吗?您可以添加R.layout.activity\u main的xml代码吗?这样我就可以看到其中是否有bug吗?我已经尝试过了,但它不起作用。:/i在布局层次结构的顶部有一个线性布局
请保存它或使用其他布局类型更改它以使此代码正常工作?不要忘记删除这一行“setContentView(R.layout.activity_main);”只要尝试用我的代码替换onCreate(),看看会发生什么?我已删除setContentView(R.layout.activity_main)
还试图复制代码,但仍不起作用。:/I已尝试此操作,但不起作用。:/I在布局层次结构的顶部有一个线性布局。
是否应删除它或使用其他布局类型更改它以使此代码起作用?不要忘记删除此行“setContentView(R.layout.activity_main);”只需尝试用我的代码替换onCreate(),看看会发生什么?我已删除setContentView(R.layout.activity\u main)
并尝试复制仍无法工作的代码:/