簡體   English   中英

如何將自定義表格布局添加到.xml布局?

[英]How to add customized tablelayout into .xml layout?

我要怎么做才能將此布局添加到main.xml中。 該類文件與主要活動類無關。 我是否需要將此類調用為主活動類以添加此動態表格布局。 告訴我如何將這個課程添加到主要活動課程中

import android.R.color;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TableRow.LayoutParams;

public class TestGridActivity extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

        TableLayout layout = new TableLayout (this);
        layout.setLayoutParams( new TableLayout.LayoutParams(85,85) );

        layout.setPadding(8,8,8,8);

        for (int f=0; f<=6; f++) {

            TableRow tr = new TableRow(this);

           tr.setBackgroundColor(Color.BLACK);
           tr.setPadding(0,0, 0,2 );

           TableRow.LayoutParams llp = new        TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
           llp.setMargins(0, 0, 2, 0);//2px right-margin

           //New Cell

           for (int c=0; c<=288; c++) {

               LinearLayout cell = new LinearLayout(this);
               cell.setBackgroundColor(Color.WHITE);
               cell.setLayoutParams(llp);//2px border on the right for the cell

                TextView b = new TextView (this);
                b.setText("Sample");
                b.setTextSize(10.0f);
                    b.setHeight(60);
                    b.setWidth(70);
                b.setPadding(0, 0, 4, 0);

                cell.addView(b);
                tr.addView(cell);

           } // for
            layout.addView(tr);
        } // for

        super.setContentView(layout);
    } // ()

    public void onClick(View view) {
        ((TextView) view).setText("*");
        ((TextView) view).setEnabled(false);
    }
 } // class

我應該怎么做才能將此表布局傳遞到main.xml布局中

將main.xml設置為contentview並將動態表格布局作為視圖添加到main中聲明的布局中。 xml。

因此您的onCreate()代碼將如下所示

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
LinearLayout ll = (LinearLayout) findViewById(R.id.add_layout);

            TableLayout layout = new TableLayout (this);
            layout.setLayoutParams( new TableLayout.LayoutParams(85,85) );

            layout.setPadding(8,8,8,8);

            for (int f=0; f<=6; f++) {

                TableRow tr = new TableRow(this);

               tr.setBackgroundColor(Color.BLACK);
               tr.setPadding(0,0, 0,2 );

               TableRow.LayoutParams llp = new        
                       TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
               llp.setMargins(0, 0, 2, 0);//2px right-margin

               //New Cell

               for (int c=0; c<=288; c++) {

                   LinearLayout cell = new LinearLayout(this);
                   cell.setBackgroundColor(Color.WHITE);
                   cell.setLayoutParams(llp);//2px border on the right for the cell

                    TextView b = new TextView (this);
                    b.setText("Sample");
                    b.setTextSize(10.0f);
                        b.setHeight(60);
                        b.setWidth(70);
                    b.setPadding(0, 0, 4, 0);

                    cell.addView(b);
                    tr.addView(cell);

               } // for
                layout.addView(tr);
            } // for
            ll.addView(layout);
//          super.setContentView(layout);
        } // ()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM