問題
通過編程的地方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
。提前致謝。
generateViewId需要我移動到更高的API。我寧願不這樣做,除非它真的需要。 – Bono 2013-05-10 16:43:38
我認爲主要問題是您重新使用LayoutParams對象(我注意到後編輯了我的答案)。 – devconsole 2013-05-10 16:45:59
謝謝,我編輯了,我實際上認爲它可以解決它(多次犯這個錯誤),但不幸的是它沒有。 – Bono 2013-05-10 16:46:34