我以编程方式通过 TableLayout (id=myProducts_tableLayout) 创建了一个表,但它没有显示在屏幕上。下面是 XML 代码。
#`<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffffff">
<!-- Header Starts-->
<LinearLayout android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header_gradient"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<!-- Logo Start-->
<ImageView android:src="@drawable/real_time_bidding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"/>
<!-- Logo Ends -->
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<LinearLayout android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="@layout/footer"
android:layout_alignParentBottom="true">
</LinearLayout>
<!-- Footer Ends -->
<!-- All Products View -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_below="@id/header">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#153E7E"
android:textSize="25px"
android:textStyle="bold"
android:typeface="serif"
android:gravity="center"
android:text="My Products"/>
<TableLayout
android:id="@+id/myProducts_tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="0"
>
</TableLayout>
<!-- Link to Enter New Product -->
<TextView android:id="@+id/link_to_allProducts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="213dip"
android:layout_marginBottom="50dip"
android:textColor="#153E7E"
android:textSize="20px"
android:textStyle="bold|italic"
android:typeface="serif"
android:gravity="center"
android:text="Back to All Products"/>
</LinearLayout>
<!-- All Products View Ends -->
</RelativeLayout>
</ScrollView>
`
下面是.java代码
super.onCreate(savedInstanceState);
setContentView(R.layout.my_products);
String[] sampleTxt = {"txt1", "txt2", "txt3"};
ScrollView sv = new ScrollView(this);
final TableLayout layout = TableLayout findViewById(R.id.myProducts_tableLayout);
layout.setLayoutParams(new TableLayout.LayoutParams(4, 5));
for(int i = 0; i < sources.length; i++)
{
final TableRow tr = new TableRow(this);
final TextView t = new TextView(this);
final Button b = new Button(this);
final LayoutParams textparams = new LayoutParams(400,40); // Width , height
t.setLayoutParams(textparams);
t.setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
t.setTextColor(Color.BLUE);
final LayoutParams btnparams = new LayoutParams(20,20);
btnparams.setMargins(10, 20, 15, 0); // (left, top, right, bottom)
b.setLayoutParams(btnparams);
b.setBackgroundResource(R.drawable.delete);
t.setText(sampleTxt[i]);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
layout.removeView(tr);
}
});
tr.addView(b);
tr.addView(t);
layout.addView(tr);
}
任何人都可以指出错误。而且我也不知道为什么 Eclipse 要求将 TableLayout、TableRow、TextView、Button 设为“final”,否则它会给我一个错误。
谢谢