以编程方式将TextView放在RelativeLayout中彼此之下

时间:2013-05-10 16:19:59

标签: java android android-layout layoutparams

问题

<小时/> 我一直在尝试将几个TextView放在彼此之下,但我似乎无法让它工作。它们总是相互重叠,而不是放在彼此之下。

我尝试了什么

<小时/> 我试过搞乱引力(它不适用于RelativeLayout),以及各种布局参数。我得出结论,对我来说最好的解决方案是使用RelativeLayout.BELOW参数。唯一的问题是我试图找到前一个TextView的ID。

我使用迭代器为TextView分配了id。似乎我不能使用iterator - 1来计算id(尽管SO上的其他答案表明这是有效的)。我甚至尝试将当前TextView分配给“previous_tv”变量以在下一次迭代中使用。

我尝试使用TextView找到我刚放置的this.findViewByID以及id的正确迭代器值,这也无效。

代码

<小时/> 我试图找出我应该放置什么作为我的参数规则的id。这是我正在编写的代码 - 编辑,根据请求放置完整代码 - :

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); //Don't show application name on the top of the screen (valuable space)
    setContentView(R.layout.activity_task_play); //Set which layout file to use for this activity

    //Get the Task that was sent by TaskActivity
    this.task = (Task) this.getIntent().getSerializableExtra("TASK_OBJECT");
    //Get the Sums for this Task
    this.sums = this.task.getSums();

    //GridView setup
    this.gridview = (GridView) this.findViewById(R.id.gridView_task_play);

    ArrayAdapter<String> adapter = this.task.getArrayAdapterForGridView(this);

    this.gridview.setAdapter(adapter);

    //TextView setup
    RelativeLayout layout = (RelativeLayout) this.findViewById(R.id.activity_task_play_relative);

    for(int i = 0; i < this.sums.length; i++)
    {
        //Layout parameters
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);

        //This variable counts how many times the placeholder text is skipped and a operator (+-/ etc.) is placed in the sum_text.
        //This is usefull for placing the correct placeholder for each number in the sum (a, b, c etc.)
        int times_skipped = 0; 

        TextView tv = new TextView(this);

        String sum_text = "";

        for(int j = 0; j < this.sums[i].getVariables().length; j++)
        {               
            if(this.isParsable(this.sums[i].getVariables()[j]))
            {
                sum_text += TaskPlayActivity.PLACEHOLDERS[(j - times_skipped)] + " ";
            }
            else
            {
                sum_text += this.sums[i].getVariables()[j] + " ";
                times_skipped++;
            }
        }

        if(i > 0) 
        {
            params.addRule(RelativeLayout.BELOW, i - 1);
        }
        tv.setId(i);
        tv.setText(sum_text + "= " + this.sums[i].getAnswer());
        tv.setTextColor(TaskPlayActivity.COLOURS[i]);
        tv.setTextSize(25);
        tv.setLayoutParams(params);
        layout.addView(tv);
    }
}



XML

添加了XML。我的布局不是很好,所以很可能是故障存在于此。     

<RelativeLayout 
    android:id="@+id/activity_task_play_relative"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="220dip"
        android:layout_alignParentBottom="true">

        <GridView
            android:id="@+id/gridView_task_play"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="#EFEFEF"
            android:horizontalSpacing="1dp"
            android:numColumns="3"
            android:paddingTop="1dp"
            android:verticalSpacing="1dp" >

        </GridView>

    </RelativeLayout>

</RelativeLayout>

我们也欢迎任何其他建议,但我更愿意保留RelativeLayout。提前谢谢。

2 个答案:

答案 0 :(得分:1)

View#setId的文档说标识符应该是正数,因此您应该确保不使用零作为标识符值。

此外,您还必须为每个TextView创建一个新的LayoutParams实例。因为所有TextView都共享相同的LayoutParams对象,并且对该对象的更改会影响所有TextView。

您可以使用View#generateViewId生成ID并记住上次迭代的ID。

答案 1 :(得分:0)

好像你在循环中调用这段代码。在这种情况下,只需将i - 1(先前的视图ID)作为规则的id。