问题
<小时/> 我一直在尝试将几个TextView
放置在彼此下方,但我似乎无法使其工作。它们总是彼此重叠,而不是放置在彼此下方。
我尝试了什么
<小时/> 我尝试过使用重力(它不适用于RelativeLayout)和各种布局参数。我得出的结论是,对我来说最好的解决方案是使用RelativeLayout.BELOW
参数。唯一的问题是我试图找到前一个 TextView
的 id。 我使用迭代器为
TextView
分配 id。看来我不能使用迭代器 - 1 来算作 id (尽管 SO 上的其他答案表明这是可行的)。我什至尝试将当前的 TextView 分配给“previous_tv”变量以在下一次迭代中使用。 我尝试使用
this.findViewByID
和 id 的正确迭代器值查找刚刚放置的 TextView
,但这也不起作用。
代码
<小时/> 我正在尝试找出应该放置什么作为我的参数规则的 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
。提前致谢。
最佳答案
View#setId 的文档表示标识符应该是正数,因此您应该确保不要使用零作为标识符值。
此外,您还必须为每个 TextView 创建一个新的 LayoutParams 实例。因为所有 TextView 共享相同的 LayoutParams 对象,并且对该对象的更改会影响所有 TextView。
你可以使用View#generateViewId生成一个ID并记住最后一次迭代的ID。
关于java - 以编程方式将 TextView 放置在相对布局中的彼此下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16486640/