Android 如何以编程方式将多个文本视图添加到RelativeLayout

Android 如何以编程方式将多个文本视图添加到RelativeLayout,android,android-relativelayout,overlapping,Android,Android Relativelayout,Overlapping,如何通过编程方式,通过一次单击一个按钮,将多个TextView添加到现有RelativeLayout中,而不使TextView相互重叠 我正在尝试这样的事情- onCreate()方法中存在以下代码: 文本视图被添加到RelativeLayout中,但它们彼此重叠,如何解决这个问题 在运行应用程序之前,为什么不在xml文件中添加所有文本视图(尽可能多)。只需将您最初不想显示的文本视图的可见性设置为“GONE”,然后在按钮单击listener中继续将文本视图的可见性更改为“View.Visibl

如何通过编程方式,通过一次单击一个按钮,将多个TextView添加到现有RelativeLayout中,而不使TextView相互重叠


我正在尝试这样的事情- onCreate()方法中存在以下代码:


文本视图被添加到RelativeLayout中,但它们彼此重叠,如何解决这个问题

在运行应用程序之前,为什么不在xml文件中添加所有文本视图(尽可能多)。只需将您最初不想显示的文本视图的可见性设置为“GONE”,然后在按钮单击listener中继续将文本视图的可见性更改为“View.Visible”。
如果希望每次按下按钮时都显示一个新的文本视图,则将计数器设置为按钮,并且每次计数器递增时,您都会将所需文本视图的可见性更改为View.Visible。如果你明白我说的话,你将能够自己编写代码

目标是通过编程方式,通过一次单击一个按钮,将多个文本视图添加到现有RelativeLayout中,并且文本视图不会相互重叠

这就是我最终得出的结论,这是可行的,但我不确定这是否是最好的方法(甚至是一种好方法)


onCreate()方法中存在以下代码:

要记住的一些事情:

  • RelativeLayout.LayoutParams的参数很重要,请参阅。选择WRAP_内容是为了满足我的需要,因为它会导致TextView只占用文本的大小,而不是整个父视图的大小。。。在我改变这个之前,重叠已经发生了

  • 如果以后要为新布局参数引用每个新TextView,则必须设置其id

  • id必须是正数,零不是正数

  • RelativeLayout保存并处理TextView,textViewArray只是为了在需要时存储和引用每个TextView的ID

对应的XML在主父级中具有以下特性:

     <RelativeLayout 
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".2" >

        <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"                 
                android:text="@string/die_one" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name_a_button" />
    </RelativeLayout>

请注意,第一个RelativeLayout和TextView都有一个id,因此活动中的addRule()方法可以引用它们


此代码允许用户单击按钮并将新文本视图添加到相对视图中,而不会使它们重叠。

您可以使用方向为垂直的线性布局,而不是相对布局。它将使所有文本视图一个接一个地垂直对齐。
我不考虑将大量文本视图添加到XML文件中,这是一个有效的解决方案,因为用户单击一个按钮的次数是未知的。你的问题是什么?如果您试图编写一个FAQ风格的条目,那么“问题”中的所有内容都应该包含在答案中,并且该问题应该是一个实际的堆栈溢出适当的问题,解释问题。顺便说一句,Commonware的评论是有效的。我最初的帖子没有足够直接地问这个问题,我已经编辑了帖子…我喜欢这种方法,但担心如果活动中有太多的文本视图,它可能会减慢应用程序的速度。会吗?另外,我不知道用户将创建多少文本视图。。有一件事是肯定的。它不会减慢应用程序的速度。但是,当文本视图的数量未知时,这是一个问题(对我来说,因为我也是新手)。对不起,我帮不了你,兄弟!你的回答确实有帮助(因为我是新来的,所以我不能投票),谢谢。不过你可以接受答案。你接受的越多,人们下次回答你问题的机会就越大。
// Creates a variable to keep track of the amount of TextViews, the 
// first TextView, an array, and then stores the TextView in the Array 
int numberOfTextViews = 1;
TextView[] textViewArray = new TextView[20];
TextView textViewToSeeFirst = (TextView) findViewById(R.id.textView1);
textViewArray[numberOfTextViews - 1] = textViewToSeeFirst;

// Also need to reference the RelativeLayout we are putting TextViews in
RelativeLayout rLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);

Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
                    (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
        lparams.addRule(RelativeLayout.RIGHT_OF, textViewArray[numberOfTextViews - 1].getId());
        lparams.addRule(RelativeLayout.ALIGN_TOP, textViewArray[numberOfTextViews - 1].getId());

        TextView newTextView = new TextView(TheActivityYouAreUsingActivity.this);
        newTextView.setText("text you want");
        newTextView.setId(numberOfTextViews);
        textViewArray[numberOfTextViews] = newTextView;
        rLayout1.addView(textViewArray[numberOfTextViews], lparams);
        numberOfTextViews = numberOfTextViews + 1;

    }
     <RelativeLayout 
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".2" >

        <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"                 
                android:text="@string/die_one" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name_a_button" />
    </RelativeLayout>