Android:动态生成的视图已添加但未显示

时间:2017-02-25 01:52:19

标签: android

我有一个这种格式的UI组件:

    <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;
}

1 个答案:

答案 0 :(得分:0)

如何生成viewId? 您可以使用HierarchyViewer或启用“开发者选项”中的Show layout bounds来调试此内容。

我认为您应该尝试使用这段代码生成视图ID:

public static int generateViewId() {
   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        for (; ; ) {
            final int result = sNextGeneratedId.get();
            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) {
                newValue = 1; // Roll over to 1, not 0.
            }
            if (sNextGeneratedId.compareAndSet(result, newValue)) {
                return result;
            }
        }
    } else {
        return View.generateViewId();
    }
}