【问题标题】:adding table layout programmatically in android在android中以编程方式添加表格布局
【发布时间】:2015-02-08 13:17:45
【问题描述】:

我正在以编程方式在线性布局中添加表格,但它没有显示在屏幕上。 下面是代码-

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);

【问题讨论】:

  • mChartLayout.requestLayout();
  • @Suvtruf 不工作。
  • 显示布局xml,mChartLayout是什么?
  • @Suvtruf 我添加了 xml 布局
  • @MikeM。没有得到你想说的准确

标签: android layout android-linearlayout tablelayout tablerow


【解决方案1】:

我可以使用此代码添加表格:

    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);

添加其他必需的属性

希望对你有所帮助ツ

【讨论】:

  • 您的代码正在运行,但我希望将 3 个文本视图水平放置在表格行中
  • 显示所需的输出
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 2017-12-10
  • 1970-01-01
  • 2016-05-24
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 2013-07-05
相关资源
最近更新 更多