【发布时间】: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