【发布时间】:2016-04-09 20:04:53
【问题描述】:
我有一个 Open GL ES 2.0 应用,并在我的 GLSurfaceView 顶部显示一个 Android TextView。
我的实际文本视图显示正常,但现在我需要尝试将它们居中。
这是我目前所拥有的:
text1 = new TextView(this);
text1.setText("This is some sample text");
text1.setTextColor(Color.WHITE);
text1.setTextSize(textSize);
text2= new TextView(this);
text2.setText("And this is some more sample text");
text2.setTextColor(Color.WHITE);
text2.setTextSize(textSize);
RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
textLayout = new RelativeLayout(this);
textLayout.setLayoutParams(textParams);
textLayout.addView(text1);
textLayout.addView(text2);
mainLayout.addView(textLayout);
但是,当我运行它时,只有 text1 居中。文本 2 不是,它从第一个(正确居中的)textView 的左侧开始。尽我所能,我似乎无法让他们两个都居中。下图描述了我的意思:
(请注意重新垂直放置 - 在我的 实际 结果中,两个 TextView 处于相同的垂直/'y' 位置,因此重叠 - 显然这是预期的我没有改变第二个TextView的垂直位置,但是为了让事情更清楚说明,我已经手动将第二个向下移动了......)
我的想法是创建一个“textLayout”,我可以在其中添加我所有的 textView,然后将该 textLayout 添加到我的主布局中。因此,只需一行代码即可方便地添加和删除所有 textView。
请注意,我没有使用,也不希望为此使用 XML - 我想知道如何以编程方式执行此操作。
我错过了什么?
编辑这是我创建主布局的方式,我正在向其中添加 textView.....
layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
【问题讨论】:
标签: android layout textview android-relativelayout