问题
通过编程的地方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