dynamically creating tablelayout (android)

亡梦爱人 提交于 2019-12-01 18:05:14

Here is the working code:

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    TextView tv1 = new TextView(this);
    TextView tv2=new TextView(this);
    android.widget.TableRow.LayoutParams trparams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT);
    tv1.setLayoutParams(trparams);
    tv2.setLayoutParams(trparams);
    tv1.setText("Hello1!");
    tv2.setText("Hello2!");
    TableLayout layoutINNER = new TableLayout(this);
    layoutINNER.setLayoutParams(params);
    TableRow tr = new TableRow(this);

    tr.setLayoutParams(params);
    tr.addView(tv1);
    tr.addView(tv2);
    layoutINNER.addView(tr);
    LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
    main.addView(layoutINNER);
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!