【发布时间】:2012-05-11 05:15:59
【问题描述】:
所以我想在线性布局中居中放置一个文本视图,其中我有 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); 之类的事情。它没有奏效。
【问题讨论】:
-
a) 让 textview 在宽度上 match_parent 并使用重心 b) 在 textview 布局参数上将 layout_gravity 设置为 center(对不起,我通常在 XML 中这样做)!
-
回答了我的问题。将其发布为答案,我会接受!
标签: android textview android-linearlayout