0
我有這種格式的UI組件:的Android:動態生成的視圖添加,但沒有示出
<RelativeLayout
android:id="@+id/item_lists"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/first_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="First List"/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/first_list"
android:divider="@android:color/transparent"
android:dividerHeight="10dp" >
</ListView>
</RelativeLayout>
外RelativeLayout的是我打算動態地生成的觀點和注入到包裝器,該邏輯動態地添加次(文本標題和另一個列表視圖):
private void populateLists() {
View pre = firstList; // this points to the list view "list_view"
if(!titleMap.isEmpty()) { // titleMap is a hashmap that maps titles to lists
Set<String> titles = getTitles();
for(String title : titles){
List<Item> li = titleMap.get(title);
TextView tag = generateTitleTextView(title, pre);
ListView lv = generateListView(li, tag);
sceneLists.addView(tag);
sceneLists.addView(lv);
pre = lv;
}
}
}
generateTitleTextView(String title, View pre)
和generateListView(List<Item> list, View pre)
是生成的觀點和將View成分下所生成的視圖,通過使用
layoutParams.addRule(RelativeLayout.BELOW, pre.getId());
代碼可以無例外地運行,但生成的視圖在頁面上不可見,我想知道可能是什麼問題?提前致謝!
編輯:爲generateTitleTextView代碼,generateListView類似於唯一的區別是它會產生一個ListView
private TextView generateTitleTextView(String title, View pre) {
TextView tag = new TextView(this);
tag.setId(generateRandomViewId());// generates a random int as Id
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.BELOW, pre.getId());
tag.setLayoutParams(layoutParams);
tag.setText(title);
return tag;
}
我使用隨機UUID的哈希碼生成viewId。我想這不會是一個問題?我會根據你的建議嘗試調試,謝謝! –
我認爲你所建議的代碼是使用View.generateViewId()的實現邏輯,它沒有幫助我的情況。 –