I am trying to create a LinearLayout with TextViews in Java, because the number of elements is dynamically specified so using XML won't work out for me.
Here is a little sample of my Code:
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);
}
}
When i start this activity it does not show this TextView but it also does not show an error.
Does anyone have an advice?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…