我建立類似一個ListView,但我我自己的滾動,因爲我想要做一些定製的東西,比使用ArrayAdapters更多的控制。問題的建築佈局編程
所以我在XML定義我的佈局的一部分,包括滾動型內一個LinerLayout。我的目標是在代碼中綁定到該Linearlayout,然後在LinearLayout中使用任何XML(僅限代碼)插入其他RelativeLayouts。
這裏是我的XML:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ListScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/ListHolder"
android:layout_height="400px"
android:background="#323232"
>
<!--Here is where I want the RelativeLayouts to go... -->
</LinearLayout>
</ScrollView>
然後在代碼中,我嘗試添加RelativeLayouts,高度每50像素,在LinearLayout中,上面說的一個具有400像素的高度。
//The parent container - is defined above in XML.
itemContainer = new LinearLayout(context);
itemContainer = (LinearLayout)findViewById(R.id.ListHolder);
Layouts = new ArrayList<RelativeLayout>();
Layouts = LoadWithRelativeLayouts();
for(RelativeLayout listItem: Layouts){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 40);
listItem.setLayoutParams(params);
itemContainer.addView(listItem);
}
陣列中的佈局中的每一箇中有一個文本視圖,上面寫着「測試」。當我通過代碼,有陣列中10個元素,並且所有的textviews都在那裏,所以我希望看到的400像素的LinearLayout填充有10個相對佈局一個接着一個,每個具有50像素的高度(和FILL_PARENT寬度)閱讀「測試」 - 但我看到的只有一個,就好像只有一個被添加了,或者它們都被放置在另一個之上。
現在獲得的截圖...
你確定所有10個存在?你是否已經完成了調試器? – 2011-03-05 17:00:54
@Nick Campion - 是的,陣列裏充滿了10個元素 - 我完成了它。我以同樣的方式加載每個元素,並帶有「Test」文本視圖。我只看到一個項目,說:「測試」 - 這是研究背景是顏色#323232由XML定義的 - 但它只是40PX的高度,不應該#323232在高度400像素? – tpow 2011-03-05 17:04:08
活計,你需要設置你的'LinearLayout'的方向... – techiServices 2011-03-05 17:06:26