android - Android 中的动态表格布局?

标签 android

我尝试从 Java 代码添加表格布局。当一个表行中有两个字段时,它只显示第一个字段。如何显示包含所有字段的整行?

  .....
  ....
  for(int idx=0;idx<4;idx++){
      TableRow tbrow   = new TableRow(this);

      TextView text_v1 = new TextView(this);
      text_v1.setText("TextView");

      TextView text_v2 = new TextView(this);
      text_v2.setText(" Idx : "+idx);

      tbrow.addView(text_v1);
      tbrow.addView(text_v2);

      linearlayout.addView(tbrow);
  }
  .........
      ........

如何做到这一点?

最佳答案

LinearLayout ll = new LinearLayout(YourActivity.this);
        ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        ll.setBackgroundColor(Color.parseColor("#000000"));
        ll.setOrientation(LinearLayout.VERTICAL);

    ScrollView sv = new ScrollView(YourActivity.this);
    sv.setSmoothScrollingEnabled(true);

    TableLayout tl = new TableLayout(YourActivity.this);
    tl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    tl.setStretchAllColumns(true);

    TextView header= new TextView(YourActivity.this);
    header.setText("This is heading ");
    header.setTextColor(Color.WHITE);
    header.setBackgroundColor(Color.parseColor("#323232"));
    header.setTextSize(17.0f);
    header.setGravity(Gravity.CENTER);
    header.setClickable(true);

    for(int sub = 0; sub <=6; sub++) { // Six Rows will be genrated.
    TableRow tr = new TableRow(YourActivity.this);
    tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    if(sub != 0 ){
        TextView col1 = new TextView(YourActivity.this);
        col1.setText("Column1");
        col1.setTextSize(17.0f);
        col1.setPadding(5, 0, 0, 2);
        col1.setTextColor(Color.GRAY);
        col1.setGravity(Gravity.CENTER_VERTICAL);
        col1.setLayoutParams(new LayoutParams(50,50));
        tr.addView(col1);

        TextView col2 = new TextView(YourActivity.this);
        col2.setText("Column2");
        col2.setTextSize(17.0f);
        col2.setPadding(0, 2, 0, 2);
        col2.setTextColor(Color.GRAY);
        col2.setGravity(Gravity.CENTER_VERTICAL);
        col2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,50));
        tr.addView(col2);

        View v = new View(YourActivity.this);
        v.setBackgroundColor(Color.GRAY);
        v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,1));
        tl.addView(v);
    } 
        tl.addView(tr);// add rows to the table.
     }
        TableRow row1 = new TableRow(Activity.this); // seventh row with //only one column 
        ImageButton startButton = new ImageButton(YourActivity.this);
        startButton.setBackgroundColor(Color.TRANSPARENT);
        startButton.setImageResource(R.drawable.icon);
        startButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        row1.addView(startButton);




row1.setGravity(Gravity.CENTER);
        tl.addView(row1);

        sv.addView(tl); //add table to scrollview to make it scrollable.
            ll.addView(header); //  add heading to the LinearLayout
            ll.addView(sv);     // add scrollview to LinearLayout
            setContentView(ll);  enter code here

关于android - Android 中的动态表格布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3193660/

相关文章:

android - 修改扇区尾部 Mifare Classic 的访问位

android - 在 Apportable 的 StoreKit 和 StoreKitAmazon 中测试本地化价格

java - 设计问题 : enhance code reusability specific case

android - 你如何循环线程?

android - 从 backstack 恢复 Fragment 时更改 EditText 值

android - 检查站点以显示通知

Android 应用程序在 1 分钟后未使用手机时启动另一个应用程序

android - 启动后,崩溃

android - 库使用了错误的 R 文件

java - TabHost 和 FontAwesome 图标