目前我想用 java 代码制作一个自定义 UI,而不用担心 xml 文件。我现在想在我的 linearLayout 中已经存在的 textView 下添加一个 textView。这是我到目前为止所拥有的。
View linearLayout = findViewById(R.id.rockLayout);
ImageView mineralPicture = new ImageView(this);
TextView mineralName = new TextView(this);
TextView mineralProperties = new TextView(this);
mineralProperties.setText("The properties are: " + Statics.rockTable.get(rockName).getDistinctProp());
mineralProperties.setId(2);
mineralName.setText("This mineral is: " + rockName);
mineralName.setId(1);
mineralName.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.MATCH_PARENT));
mineralProperties.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.MATCH_PARENT));
/** Need to figure out the picture....
* mineralPicture.setId(2);
* mineralPicture.setImageResource(R.drawable.rocks);
* mineralPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
*/
((LinearLayout)linearLayout).addView(mineralName);
((LinearLayout)linearLayout).addView(mineralProperties);
问题是它只添加了矿物名称文本视图,而不是矿物属性文本视图。我希望它是最顶部的 MineralName textView,然后是它下方的 MineralProperties textView。