在java代码中设置RelativeLayout
我很难让两个文本视图在我的 java 代码中出现在彼此之上。这是我正在试验的代码:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
layout = new RelativeLayout(this);
text1 = new TextView(this);
text1.setText("1");
text2 = new TextView(this);
text2.setText("2");
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
q.addRule(RelativeLayout.BELOW, layout.getId());
text1.setLayoutParams(q);
layout.addView(text1);
p.addRule(RelativeLayout.BELOW,text1.getId());
text2.setLayoutParams(p);
layout.addView(text2);
setContentView(layout);
}
这将两个文本视图堆叠在同一行上,但我希望 TextView text2 出现在 TextView text1 下方,因此在我的应用程序中,我希望以下内容显示为输出:
1
2
我已经尝试过与“addRule”方法有关的所有事情,我不确定为什么这不起作用。我想知道如何在没有 XML 的情况下做到这一点,因为我计划构建一个方法库,可以构建一个可以通过编辑数组轻松调整的布局。
I'm having a hard time getting two text views to appear on top of each other in my java code. Here's the code I'm experimenting with:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
layout = new RelativeLayout(this);
text1 = new TextView(this);
text1.setText("1");
text2 = new TextView(this);
text2.setText("2");
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
q.addRule(RelativeLayout.BELOW, layout.getId());
text1.setLayoutParams(q);
layout.addView(text1);
p.addRule(RelativeLayout.BELOW,text1.getId());
text2.setLayoutParams(p);
layout.addView(text2);
setContentView(layout);
}
This stacks the two text views on the same line, but I want TextView text2, to appear below TextView text1, so in my app I want the following to appear as the output:
1
2
I've tried all sort of things with the "addRule" method, I'm not sure why this isn't working. I want to know how to do this without XML because I plan to build a library of methods that can build up a layout that is easily adjustable through editing an array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
TextViews
没有 id(默认情况下 id 为-1
)...将其放在初始化之后:Your
TextViews
don't have an id (by default the id is-1
)... put this after their initialization:我不认为您希望将 text1 视图布局在relativelayout 下方,因为您将所有视图作为子视图添加到其中,对吗?尝试删除第一条规则;该规则要求文本视图位于其所在视图的下方。
编辑:还有一个帮助是显式设置您要相对于布局的视图的 id。
所以这里:
I don't think you are looking to layout the text1 view below the RelativeLayout since you added all your views to it as children, right? Try removing the first rule; that rule is asking the text view to be below the same view it is in.
EDIT: Also a help is explicitly setting the id of the view you are laying out relative to.
So here:
您可以为此使用 xml 布局:
在相对布局中
你设置第一个textview并为其分配一些id
对于下一个文本视图,我们可以分配参数
android:layout_below="上方文本视图的id"
这样我们就可以在第一个文本视图下面得到第二个文本视图
you can use xml layout for this :
in relative layout
u set the first textview and assign it some id
fot the next text view we can assign parameter
android:layout_below="id of above text view"
in this way we get 2nd text view below 1st text view