所以我想在线性布局中居中文本视图,其中我有 2 个其他对象填充线性布局。我有一个 imageView 和另一个 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.setGravity(Gravity.CENTER);
mineralName.setId(1);
mineralName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mineralProperties.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mineralPicture.setId(3);
mineralPicture.setImageResource(R.drawable.rocks);
mineralPicture.setAdjustViewBounds(true);
mineralPicture.setMaxHeight(100);
mineralPicture.setMaxWidth(200);
mineralPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
((LinearLayout)linearLayout).addView(mineralName);
((LinearLayout)linearLayout).addView(mineralProperties);
((LinearLayout)linearLayout).addView(mineralPicture);
我应该提一下,我已经尝试过诸如 MineralName.setGravtiy(Gravity.CENTER); 之类的事情。它没有奏效。