我是android新手,我正在尝试以编程方式创建RelativeLayouts(至少我认为如果我的值是动态的,这是唯一可行的方法)。本质上,我构建了一个 Web 服务来返回一个值列表,并且我想显示这些值。它可以是一个值,也可以是 500。
在我的 layout.xml 中,我能够构建一个相对接近我想要的相对布局,但我只能填充一次。下面是我的 XML:
注意:这是整个 XML,但有问题的节点是 RelativeLayout。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical"
android:id="@+id/linear">
<Spinner
android:id="@+id/band"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:dropDownSelector="#000000"
android:textColor="#F0F0F0" />
<Spinner
android:id="@+id/yearSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#F0F0F0" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip"
android:id="@+id/showRelativeLayout">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Simple application that shows how to use RelativeLayout" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="My Application"
android:id="@+id/thirdLine" />
</RelativeLayout>
</LinearLayout>
所以我然后尝试在我的活动中构建相同的内容,以便我可以遍历我的网络服务结果。下面是被多次调用的方法(对于返回的每个 json 数组)。不幸的是,行为是只有一个 relativeLayout 并且它试图占据屏幕的其余部分(大约 90% 为一行)。我绝对没有理解一些基本的东西。
private void processRelativeLayout(int count) {
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.setLayoutParams(lay);
//Set the Image
//TODO: pull from webservice to get the artist image
RelativeLayout.LayoutParams imlay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
imlay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
imlay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imlay.setMargins(0, 0, 6, 0);
ImageView iv = new ImageView(this);
iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
iv.setId(1+count);
iv.setLayoutParams(imlay);
rl.addView(iv);
RelativeLayout.LayoutParams tv1lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 26);
tv1lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
tv1lay.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tv1lay.addRule(RelativeLayout.RIGHT_OF, 1+count);
TextView tv = new TextView(this);
tv.setSingleLine();
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setTextColor(Color.YELLOW);
tv.setText("Simple application that shows how to use RelativeLayout");
tv.setId(2+count);
tv.setLayoutParams(tv1lay);
rl.addView(tv);
RelativeLayout.LayoutParams tv2lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tv2lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
tv2lay.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tv2lay.addRule(RelativeLayout.ABOVE, 2+count);
tv2lay.addRule(RelativeLayout.RIGHT_OF,1+count);
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.YELLOW);
tv2.setGravity(Gravity.CENTER_VERTICAL);
tv2.setText("My Application");
tv2.setId(count+3);
tv2.setLayoutParams(tv2lay);
rl.addView(tv2);
rootLinear.addView(rl);
//add the relative layoutll.addView(tv, lay);
}
我还附上了我的模拟器的屏幕截图,以向您展示输出。我本来希望看到结果 5 次(因为我调用了 processRelativeLayout 5 次)。
为长篇道歉,但我想提供足够的信息。
更新:我试图发布一张图片,但网站说我不够老练 :)