我嘗試了以下代碼在android中的表格佈局,而不使用xml文件。但是我沒有在android模擬器上得到我的屏幕,但得到了錯誤,因爲「該應用程序已經停止了精確的操作,請再試一次。」在android中的表格佈局
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Tablelayout extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(params);
tv.setTextColor(Color.RED);
TableRow tr=new TableRow(this);
tr.addView(tv);
TableLayout tl=new TableLayout(this);
TableLayout.LayoutParams layoutparams=new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tl.addView(tr);
this.addContentView(tl, layoutparams);
}
}
我建議你使用調試器運行你的應用程序,看看logcat告訴你什麼。錯誤消息通常非常具有描述性。 –
Logcat顯示運行時錯誤.... –