java 在java代码中设置RelativeLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5327144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:40:10  来源:igfitidea点击:

Setting up RelativeLayout in java code

javaandroidandroid-layoutandroid-relativelayout

提问by Andi Jay

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:

我很难在我的 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);
    }

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:

这将两个文本视图堆叠在同一行上,但我希望 TextView text2 出现在 TextView text1 下方,因此在我的应用程序中,我希望以下内容显示为输出:

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.

我已经用“addRule”方法尝试了各种方法,我不确定为什么这不起作用。我想知道如何在没有 XML 的情况下执行此操作,因为我计划构建一个方法库,该库可以构建可通过编辑数组轻松调整的布局。

采纳答案by Cristian

Your TextViewsdon't have an id (by default the id is -1)... put this after their initialization:

TextViews没有 id(默认情况下,id 为-1)...在初始化后放置:

text1.setId(1111); // 1111 is just an example,
text2.setId(2222); // just make sure the id are unique

回答by Chris

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.

我不认为您希望在 RelativeLayout 下方布局 text1 视图,因为您将所有视图作为子项添加到其中,对吗?尝试删除第一条规则;该规则要求文本视图低于它所在的同一视图。

EDIT: Also a help is explicitly setting the id of the view you are laying out relative to.

编辑:另外一个帮助是明确设置您相对于布局的视图的 id。

So here:

所以在这里:

text1.setId(2);
p.addRule(RelativeLayout.BELOW,2);

回答by chinz

you can use xml layout for this :

您可以为此使用 xml 布局:

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

在相对布局中,您设置第一个 textview 并为其分配下一个文本视图的一些 id 我们可以分配参数 android:layout_below="id of above text view" 这样我们在第一个文本视图下方获得第二个文本视图