2013-05-10 27 views
0

问题

通过编程的地方TextViews海誓山盟下在RelativeLayout的


我一直在试图把一对夫妇的 TextView小号海誓山盟的下面,但我似乎无法得到它的工作。他们总是重叠在一起,而不是放在彼此之间。

有什么我试图


我已经试过搞乱重力(不带 RelativeLayout工作),以及各种布局参数。我已经得出结论,对我来说最好的解决方案是使用 RelativeLayout.BELOW参数。唯一的问题是我试图找到以前的 TextView的ID。

我使用迭代器为 TextView指定id。看来我不能使用迭代器 - 1来计数作为一个id(即使其他答案,SO建议这工作)。我甚至尝试将当前的 TextView分配给“previous_tv”变量以用于下一次迭代。

我试图找到 TextView我刚刚使用 this.findViewByID和正确的迭代器值为id,这也行不通。


我试图找出我应该把为我的参数规则的ID码。这是我reffering到-edit代码,放置完整的代码为每个请求 - :

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。提前致谢。

回答

1

View#setId的文档说标识符应该是一个正数,所以你应该确保不使用零作为标识符值。

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

您可以使用View#generateViewId生成一个ID并记住最后一次迭代的ID。

+0

generateViewId需要我移动到更高的API。我宁愿不这样做,除非它真的需要。 – Bono 2013-05-10 16:43:38

+0

我认为主要问题是您重新使用LayoutParams对象(我注意到后编辑了我的答案)。 – devconsole 2013-05-10 16:45:59

+0

谢谢,我编辑了,我实际上认为它可以解决它(多次犯这个错误),但不幸的是它没有。 – Bono 2013-05-10 16:46:34

0

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

+0

我试过了,但没有奏效(正如我所提到的)。 – Bono 2013-05-10 16:23:31

+0

你可以发布完整的代码,所以我可以尝试一些? – 2013-05-10 16:25:52

+0

但是,这应该起作用。你可以发布你的完整代码(循环),这样我们可以找出原因? – 2013-05-10 16:26:16