您好我得到了与RelativeLayout的一个问题。这个想法是在屏幕顶部的XML代码中定义一个标题栏,然后在左边的这个栏下面添加一个textview元素,但是当我在java代码中添加这个TextView时,它始终显示在屏幕的左上角显示器(意味着它被设置在标题栏上)。任何人都知道我在做什么错了?Android的动态设置元素的RelativeLayout
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main">
<Button
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
</ScrollView>
RelativeLayout rl = (RelativeLayout) this.findViewById(R.id.main);
for (int i=0; i<=5; i++) {
TextView tv = new TextView(this);
tv.setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (i == 0)
params.addRule(RelativeLayout.BELOW, R.id.title);
else
params.addRule(RelativeLayout.BELOW, i-1);
rl.addView(tv, params);
}
已解决!!!! puuuhhhh ......经过长期的斗争,我终于赢了),如果有人有兴趣的解决方案:RelativeLayout.LayoutParams P =新RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);这是一个正确的定义和for循环必须开始与I = 2(为我工作,不知道为什么!!;)) – wasp256