java 如何以编程方式在java中向表格布局添加和删除行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17031006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:46:03  来源:igfitidea点击:

how add and delete rows to table layout in java programically

javaandroidtablelayouttablerow

提问by tanhatarin tanha

I am new in java and android programing. I want to add rows(include some text box) to table layout by code and delete some of them.and finaly get their text box valus.how can i do it?

我是java和android编程的新手。我想通过代码向表格布局添加行(包括一些文本框)并删除其中的一些。最后得到他们的文本框值。我该怎么做?

回答by Esteam

Here is a simple example to do what you want:

这是一个简单的例子来做你想做的事:

Layout:

布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/table"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

</TableLayout>

Activity:

活动:

public class TableLayoutActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_layout);
    final TableLayout tableLayout = (TableLayout) findViewById(R.id.table);

    for (int i = 0; i < 5; i++) {
        // Creation row
        final TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

        // Creation textView
        final TextView text = new TextView(this);
        text.setText("Test" + i);
        text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

        // Creation  button
        final Button button = new Button(this);
        button.setText("Delete");
        button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TableRow parent = (TableRow) v.getParent();
                tableLayout.removeView(parent);
            }
        });

        tableRow.addView(text);
        tableRow.addView(button);

        tableLayout.addView(tableRow);
    }

}
}

回答by Valentin Grégtheitroade

I recently got the same problem. I fixed it like this. Suppose your TableLayout is called tableand als has this id in the xml layout.

我最近遇到了同样的问题。我是这样修的。假设您的 TableLayout 称为table,并且 als 在 xml 布局中具有此 ID。

<TableLayout
                android:id="@+id/table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

Suppose you have a list of Person objects, this is the way to populate the table:

假设您有一个 Person 对象列表,这是填充表的方法:

ArrayList<Person> persons = getPersonList(); // --> suppose getting the list from this function
TableLayout table = (TableLayout) findViewById(R.id.table);
for(Person person : persons) {
    TableRow row = new TableRow(this);
    TextView tvName = new TextView(this);
    TextView tvAge = new TextView(this);
    TextView tvMail = new TextView(this);
    tvName.setText(person.getName());
    tvAge.setText(String.valueOf(person.getAge());
    tvMail.setText(person.getMail());
    row.addView(tvName);
    row.addView(tvAge);
    row.addView(tvMail);
    table.addView(row);
}

This basically means that each person has its own row. For every property of your person, 1 column is used.

这基本上意味着每个人都有自己的行。对于您个人的每个属性,使用 1 列。

Regards

问候