在android中以编程方式添加表格布局

时间:2014-12-10 11:05:52

标签: android layout android-linearlayout tablelayout tablerow

我是以编程方式在linearlayout中添加表格但未在屏幕上显示。 下面是代码 -

public void displayTable(){
        TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
        TableLayout mTableLayout=new TableLayout(getActivity());

        TableRow mTableRow;
        LinearLayout mTableLinearLayout;
        TextView mSrNotxt,mDatetxt,mTimetxt;
        Date mDate=new Date();
        String mCDate=mDate.getDate()+"-"+(mDate.getMonth()+1)+"-"+(mDate.getYear()+1900)+"";

        mTableLayout.setLayoutParams(lp2);
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
        TableRow.LayoutParams tlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);

        for(int count=0;count<mTimeList.size();count++)
        {
            mTableRow=new TableRow(getActivity());
            mTableRow.setLayoutParams(lp);

            mSrNotxt=new TextView(getActivity());
            mDatetxt=new TextView(getActivity());
            mTimetxt=new TextView(getActivity());

            mSrNotxt.setTextColor(getResources().getColor(R.color.black));
            mDatetxt.setTextColor(getResources().getColor(R.color.black));
            mTimetxt.setTextColor(getResources().getColor(R.color.black));  

            mSrNotxt.setLayoutParams(tlp);
            mDatetxt.setLayoutParams(tlp);
            mTimetxt.setLayoutParams(tlp);

            mSrNotxt.setTextSize(12);
            mDatetxt.setTextSize(12);
            mTimetxt.setTextSize(12);

            if(count==0){
                mSrNotxt.setText("No.");
                mDatetxt.setText("Date");
                mTimetxt.setText("Time in Min.");
            }
            else{
                mSrNotxt.setText((count+1+""));
                mDatetxt.setText(mCDate);
                mTimetxt.setText(mTimeList.get(count));
            }

            mTableLinearLayout=new LinearLayout(getActivity());
            mTableLinearLayout.setLayoutParams(lp);
            mTableLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            mTableRow.addView(mTableLinearLayout);
            mTableLayout.addView(mTableRow);
        }
        System.out.println("table created");
        mChartLayout.addView(mTableLayout);
    }

chartLayout的xml视图 -

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >


            <LinearLayout
                android:id="@+id/chartView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            </LinearLayout>

    </ScrollView>

如何展示桌子?有什么办法吗?

回答 -

需要在视图中添加textview -

mTableLinearLayout.addView(mSrNotxt);
mTableLinearLayout.addView(mDatetxt);
mTableLinearLayout.addView(mTimetxt);

enter image description here

1 个答案:

答案 0 :(得分:1)

我可以使用以下代码添加Table:

    TableLayout mTableLayout = new TableLayout(this);
    mTableLayout.setLayoutParams(new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    for (int count = 0; count < 3; count++) {
        TableRow row = new TableRow(this);
        row.setLayoutParams((new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT)));

        TextView valueTV = new TextView(this);
        valueTV.setText("text : "+count);
        // valueTV.setId(5);
        valueTV.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        row.addView(valueTV);
        mTableLayout.addView(row);
    }
    mChartLayout.addView(mTableLayout);

添加其他所需属性

希望它有助于ツ