2013-05-10 28 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