我想用Java中的TextViews創建一個LinearLayout,因爲元素的數量是動態指定的,因此使用XML將無法爲我工作。 這裏是我的代碼的一個小樣本:在Java中創建LinearLayout - 元素不顯示
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TextView titleView = new TextView(this);
titleView.setWidth(LayoutParams.WRAP_CONTENT);
titleView.setHeight(LayoutParams.WRAP_CONTENT);
titleView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
titleView.setText("Hallo Welt!");
layout.addView(titleView);
setContentView(layout);
}
}
當我開始這個活動它不顯示這個TextView的,但它也沒有顯示錯誤。 有沒有人有建議?
幹得好!這是錯誤 – Jay