【问题标题】:Android TableLayout programmaticallyAndroid TableLayout 以编程方式
【发布时间】:2011-08-29 04:48:41
【问题描述】:

我学习了如何使用 XML 文件创建 UI。但是请帮助我了解如何在不使用 XML 文件的情况下以编程方式完成它,尤其是对于 LinearLayout 以外的其他文件。

【问题讨论】:

    标签: android android-tablelayout


    【解决方案1】:

    使用以下代码创建表格布局

    TableLayout tbl=new TableLayout(context);
    

    使用下面创建表格行

    TableRow tr=new TableRow(context);
    

    将视图添加到表格行中

    tr.addView(view);
    

    这里的视图可能是 TextView 或 EditText 等。

    将表格行添加到表格布局中

    tbl.addView(tr);
    

    这样,您可以在表格布局中添加更多表格行。

    【讨论】:

    • 它运行正常,但在活动屏幕上没有显示任何输出......这是我的代码
    • LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT) TableLayout layout=new TableLayout(this); TableLayout.LayoutParams layoutparams=new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);表行表行=新表行(此); TextView tv=new TextView(this); tv.setText("表格布局演示"); tv.setLayoutParams(参数); EditText et=new EditText(this); et.setHint("输入文本"); et.setLayoutParams(参数); tablerow.addView(tv); tablerow.addView(et); layout.addView(tablerow); this.addContentView(layout, layoutparams);
    • 什么是上下文,我对 xamarin 和 android 很陌生,我只使用过布局设计器
    【解决方案2】:

    下面的代码示例是Here

    public class tablelayout extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    
    //initialize a button and a counter
    Button btn;
    int counter = 0;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // setup the layout
        setContentView(R.layout.main);
    
        // add a click-listener on the button
        btn = (Button) findViewById(R.id.Button01);
        btn.setOnClickListener(this);        
    
    }
    
    // run when the button is clicked
    public void onClick(View view) {
    
        // get a reference for the TableLayout
        TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
    
        // create a new TableRow
        TableRow row = new TableRow(this);
    
        // count the counter up by one
        counter++;
    
        // create a new TextView
        TextView t = new TextView(this);
        // set the text to "text xx"
        t.setText("text " + counter);
    
        // create a CheckBox
        CheckBox c = new CheckBox(this);
    
        // add the TextView and the CheckBox to the new TableRow
        row.addView(t);
        row.addView(c);
    
        // add the TableRow to the TableLayout
        table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
    }
    

    }

    【讨论】:

      【解决方案3】:

      |*|使用 Java 代码的 3 x 3 按钮的表格布局:

      在 tblRowCwtVal 中设置行数
      设置 tblColCwtVal 中的列数
      设置字符串 |可在 tblAryVar 中绘制

      在这个例子中,我们为每个表格视图使用了按钮。您可以使用 TextView | ImageView 并进行相应修改

      int tblRowCwtVal = 3;
      int tblColCwtVal = 3;
      int[][] tblAryVar =
           {
              {R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name},
              {R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name},
              {R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name}
           };
      
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.srn_nam_uic);
      
          namRelLyoVar = (RelativeLayout) findViewById(R.id.NamSrnLyoUid);
      
          TableLayout namTblLyoVar = new TableLayout(this);
          TableLayout.LayoutParams tblLyoRulVar = new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
          TableRow.LayoutParams btnLyoRulVar = new TableRow.LayoutParams(50,50);
      
          for(int tblRowIdxVar = 0; tblRowIdxVar < tblRowCwtVal; tblRowIdxVar++)
          {
              TableRow tblRowVar = new TableRow(this);
      
              for(int tblColIdxVar = 0; tblColIdxVar < tblColCwtVal; tblColIdxVar++)
              {
                  Button namIdxBtnVar = new Button(this);
                  Drawable DrwablIdxVar = getResources().getDrawable(tblAryVar[tblRowIdxVar][tblColIdxVar]);
                  DrwablIdxVar.setColorFilter(Color.rgb(0,128,0), PorterDuff.Mode.SRC_IN);
                  namIdxBtnVar.setBackground(DrwablIdxVar);
      
                  tblRowVar.addView(namIdxBtnVar, btnLyoRulVar);
              }
              namTblLyoVar.addView(tblRowVar, tblLyoRulVar);
          }
      
          namTblLyoVar.setLayoutParams(tblLyoRulVar);
          namRelLyoVar.addView(namTblLyoVar);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-29
        • 1970-01-01
        • 2010-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多