Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中创建表布局并从代码中添加行_Android_Code Behind_Android Tablelayout - Fatal编程技术网

在Android中创建表布局并从代码中添加行

在Android中创建表布局并从代码中添加行,android,code-behind,android-tablelayout,Android,Code Behind,Android Tablelayout,我需要创建一个表布局,并从Java代码中动态添加行。我已经阅读了这里的问题,但他们提到在已经创建的表布局中添加表行(来自xml) 我需要创建表布局,并向其中动态添加数据。 有人能提供一些信息吗 现在,我已经准备好了线性布局代码,它将按钮从一个代码后面添加到另一个下面,我现在需要将其放置在表格格式下。将下面的代码添加到活动中的onCreate()方法中: @Override protected void onCreate(Bundle savedInstanceState) { super

我需要创建一个表布局,并从Java代码中动态添加行。我已经阅读了这里的问题,但他们提到在已经创建的表布局中添加表行(来自xml)

我需要创建表布局,并向其中动态添加数据。 有人能提供一些信息吗


现在,我已经准备好了线性布局代码,它将按钮从一个代码后面添加到另一个下面,我现在需要将其放置在表格格式下。

将下面的代码添加到
活动中的
onCreate()
方法中:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    TableLayout tableLayout = new TableLayout(this);
    for (int i = 0; i < 5; i++)
    {
        TableRow tableRow = new TableRow(this);

        for (int j = 0; j < 3; j++)
        {
            Button button = new Button(this);
            button.setText(""+j);
            tableRow.addView(button);
        }

        tableLayout.addView(tableRow);
    }
    setContentView(tableLayout);
}
@覆盖
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TableLayout TableLayout=新的TableLayout(本);
对于(int i=0;i<5;i++)
{
TableRow TableRow=新的TableRow(本);
对于(int j=0;j<3;j++)
{
按钮按钮=新按钮(此按钮);
按钮设置文本(“+j”);
tableRow.addView(按钮);
}
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
}

代码将向表中添加五行三个按钮,文本为1到3。

将下面的代码添加到
活动中的
onCreate()
方法中:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    TableLayout tableLayout = new TableLayout(this);
    for (int i = 0; i < 5; i++)
    {
        TableRow tableRow = new TableRow(this);

        for (int j = 0; j < 3; j++)
        {
            Button button = new Button(this);
            button.setText(""+j);
            tableRow.addView(button);
        }

        tableLayout.addView(tableRow);
    }
    setContentView(tableLayout);
}
@覆盖
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TableLayout TableLayout=新的TableLayout(本);
对于(int i=0;i<5;i++)
{
TableRow TableRow=新的TableRow(本);
对于(int j=0;j<3;j++)
{
按钮按钮=新按钮(此按钮);
按钮设置文本(“+j”);
tableRow.addView(按钮);
}
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
}

代码将在表格中添加五行,其中三个按钮的文本为1到3。

要在表格中添加三个按钮,请使用下面的代码

TableLayout tableLayout = new TableLayout(this);
    for (int i = 0; i < 10; i++)
    {
        TableRow tableRow = new TableRow(this);
        Button button = new Button(this);
        button.setText("1");
        tableRow.addView(button);

        button = new Button(this);
        button.setText("2");
        tableRow.addView(button);

        button = new Button(this);
        button.setText("3");
        tableRow.addView(button);

        tableLayout.addView(tableRow);
    }
    setContentView(tableLayout);
TableLayout TableLayout=新的TableLayout(此);
对于(int i=0;i<10;i++)
{
TableRow TableRow=新的TableRow(本);
按钮按钮=新按钮(此按钮);
按钮。设置文本(“1”);
tableRow.addView(按钮);
按钮=新按钮(此按钮);
按钮设置文本(“2”);
tableRow.addView(按钮);
按钮=新按钮(此按钮);
按钮设置文本(“3”);
tableRow.addView(按钮);
tableLayout.addView(tableRow);
}
setContentView(tableLayout);

要在TableRow中添加三个按钮,请使用下面的代码

TableLayout tableLayout = new TableLayout(this);
    for (int i = 0; i < 10; i++)
    {
        TableRow tableRow = new TableRow(this);
        Button button = new Button(this);
        button.setText("1");
        tableRow.addView(button);

        button = new Button(this);
        button.setText("2");
        tableRow.addView(button);

        button = new Button(this);
        button.setText("3");
        tableRow.addView(button);

        tableLayout.addView(tableRow);
    }
    setContentView(tableLayout);
TableLayout TableLayout=新的TableLayout(此);
对于(int i=0;i<10;i++)
{
TableRow TableRow=新的TableRow(本);
按钮按钮=新按钮(此按钮);
按钮。设置文本(“1”);
tableRow.addView(按钮);
按钮=新按钮(此按钮);
按钮设置文本(“2”);
tableRow.addView(按钮);
按钮=新按钮(此按钮);
按钮设置文本(“3”);
tableRow.addView(按钮);
tableLayout.addView(tableRow);
}
setContentView(tableLayout);

init()方法下添加以下代码:

for (int i = 0; i < GetGlobal.totalrow; i++) {
            TableRow tbrow = new TableRow(this);
            // tbrow.setLayoutParams(tableRowParams);
            TextView t1v = new TextView(this);

            t1v.setText(JSONParser.heading[i].replace('"', ' '));
            t1v.setBackgroundResource(R.drawable.diamond_detail1);
            t1v.setPadding(5, 3, 5, 3);
            t1v.setMinHeight(50);
            t1v.setTypeface(Typeface.SERIF);
            t1v.setTextColor(Color.parseColor("#FFFFFF"));
            t1v.setGravity(Gravity.FILL);
            tbrow.addView(t1v);
for(int i=0;i
init()方法下添加以下代码:

for (int i = 0; i < GetGlobal.totalrow; i++) {
            TableRow tbrow = new TableRow(this);
            // tbrow.setLayoutParams(tableRowParams);
            TextView t1v = new TextView(this);

            t1v.setText(JSONParser.heading[i].replace('"', ' '));
            t1v.setBackgroundResource(R.drawable.diamond_detail1);
            t1v.setPadding(5, 3, 5, 3);
            t1v.setMinHeight(50);
            t1v.setTypeface(Typeface.SERIF);
            t1v.setTextColor(Color.parseColor("#FFFFFF"));
            t1v.setGravity(Gravity.FILL);
            tbrow.addView(t1v);
for(int i=0;i
在动态编码GUI时不会使用布局参数…?出于好奇问一下…另外,如果我想以每行必须有3列的方式添加按钮,该怎么办?是的,您可以使用TableLayout.LayoutParams。如果您想要有3列的行,只需将3个视图添加到表行中,您可以更具体地…?用于添加吗将3个视图添加到行…?因为我将在循环的每个迭代中添加每个按钮..因此无法同时添加3个视图。请您用一个示例解释一下?检查answebelow@HussainChachuliya复制代码并将Activities onCreate方法替换为它在动态编写GUI时不会使用布局参数吗。。。??出于好奇,我只是想问一下……另外,如果我想以这样的方式添加按钮,即每行必须有3列,该怎么办?是的,您可以使用TableLayout.LayoutParams。如果您想要一行有3列,只需在表行中添加3个视图。您能更具体地说吗?…在每行中添加3个视图…?因为我会在每行中添加每个按钮循环的迭代..因此无法同时添加3个视图。能否请您用一个示例进行解释?请检查answebelow@HussainChachuliya复制代码并用创建方法替换您的活动。您可以添加一个for循环以添加3个按钮。我根据我的要求做了一些修改,我很乐意……感谢您的帮助:)你可以添加一个for循环来添加3个按钮。我根据我的要求做了一些修改,我很乐意……谢谢你的帮助。:)