main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id = "@+id/mainLayout"
    >
<TextView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name"
    />
<EditText
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:minLines = "1"
android:maxLines = "1"
android:gravity = "top|fill_vertical"
android:numeric = "decimal"
android:id="@+id/sumEdit"
/>
<TableLayout
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:id="@+id/tableLayout"
>
</TableLayout>
</LinearLayout>
public class MainAvitivy extends Activity {
private TableLayout tableLayout = null; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init() ;
}   public void init(){
tableLayout = (TableLayout) this.findViewById(R.id.tableLayout);
tableLayout.setStretchAllColumns(false);
TableRow tableRow = new TableRow(tableLayout.getContext());
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tableRow.setId(10000);
for (int i = 0; i <= 9 ; i++) {
Button button = new Button(this);
button.setText(" " + i + " ");
button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT ,ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.addView(button);
Log.i("syso" , i + "");
}
tableLayout.addView(tableRow);
}}
添加不了 ,
运行后,界面顶部只有一个TextView 和 EditText 控件
没有显示我动态添加的按钮

解决方案 »

  1.   

    刚发完 ,又好了。晕,
    解决办法
     button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT ,ViewGroup.LayoutParams.WRAP_CONTENT));
    改成
    button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT ,LayoutParams.WRAP_CONTENT));
      

  2.   

    我这里使用了,还是不行。
    用的是tablerow.layoutParams
    其他的都不显示问题是,那个没有按照内容 来显示长度