我已经在一个单独的应用程序(测试应用程序)中编写了这段代码,它工作正常..
public class MainActivity extends Activity {
String companies[] = {"Google","iPhone","Nokia","Samsung",
"Google","Windows","iPhone","Nokia","Samsung",
"Google","Windows","iPhone","Nokia","Samsung","waseela"};
String os[] = {"Android","iOS","Bada",
"Android","Mango","iOS","Symbian","Bada",
"Android","Mango","iOS","Symbian","Bada","wwwwwwww","wx"};
TableLayout tl;
TableRow tr;
TextView companyTV,valueTV;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.maintable);
addHeaders();
addData();
}
/** This function add the headers to the table **/
public void addHeaders(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
TextView companyTV = new TextView(this);
companyTV.setText("Companies");
companyTV.setTextColor(Color.GRAY);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
companyTV.setPadding(5, 5, 5, 0);
tr.addView(companyTV); // Adding textView to tablerow.
/** Creating another textview **/
TextView valueTV = new TextView(this);
valueTV.setText("Operating Systems");
valueTV.setTextColor(Color.GRAY);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 0);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// we are adding two textviews for the divider because we have two columns
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating another textview **/
TextView divider = new TextView(this);
divider.setText("-----------------");
//divider.setTextColor(Color.<span class="IL_AD" id="IL_AD11">GREEN</span>);
divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
divider.setPadding(5, 0, 0, 0);
divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider); // Adding textView to tablerow.
TextView divider2 = new TextView(this);
divider2.setText("-------------------------");
divider2.setTextColor(Color.GREEN);
divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
divider2.setPadding(5, 0, 0, 0);
divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider2); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
/** This function add the data to the table **/
public void addData(){
for (int i = 0; i < companies.length; i++)
{
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
companyTV = new TextView(this);
companyTV.setText(companies[i]);
companyTV.setTextColor(Color.RED);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
companyTV.setPadding(5, 5, 5, 5);
tr.addView(companyTV); // Adding textView to tablerow.
/** Creating another textview **/
valueTV = new TextView(this);
valueTV.setText(os[i]);
valueTV.setTextColor(Color.GREEN);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 5);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}
带有包含表格布局的 ScrollView 的 xml 文件。
问题是当我将该代码放在我正在处理的主项目中时,它什么也没有显示。我已将 xml 布局嵌入到主项目布局中,我没有收到任何错误,但没有显示任何内容。
我怎样才能得到这个工作?