我想用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