android - 一个ScrollView不能添加多个 View 吗?

标签 android android-layout dynamic android-widget android-scrollview

如果是这样,则似乎 ScrollView 相当蹩脚(值得怀疑)或者有其他方法可以做到这一点。这是我的代码。它轰炸的地方(即第二次通过循环)被评论。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ondemandandautomatic_dynamicauthorize);

    ScrollView svh = (ScrollView) findViewById(R.id.scrollViewHost);

    // Contacts data snippet adapted from
    // http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // Create a Linear Layout for each contact?
            LinearLayout llay = new LinearLayout(this);
            llay.setOrientation(LinearLayout.HORIZONTAL);

            LinearLayout.LayoutParams llp = new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            llp.weight = 1.0f;

            CheckBox cbOnDemand = new CheckBox(getApplicationContext());
            cbOnDemand.setTag(id);
            cbOnDemand.setLayoutParams(llp);
            llay.addView(cbOnDemand);

            CheckBox cbTime = new CheckBox(getApplicationContext());
            cbOnDemand.setTag(id);
            cbTime.setLayoutParams(llp);
            llay.addView(cbTime);

            CheckBox cbSpace = new CheckBox(getApplicationContext());
            cbOnDemand.setTag(id);
            cbSpace.setLayoutParams(llp);
            llay.addView(cbSpace);

            TextView tv = new TextView(getApplicationContext());
            tv.setTag(id);
            tv.setText(name);
            tv.setLayoutParams(llp);
            llay.addView(tv);

            svh.addView(llay); // it transports me to Eclipse's Debug perspective when I hit this line the SECOND time around.
            // One cat on stackOverflow said to do this, another said it
            // would be unnecessary
            svh.invalidate();
        }
    }
}

最佳答案

您可以做两件事来解决这个问题。

1) 使 ScrollView 的唯一 child 成为垂直 LinearLayout 并将所有 child 添加到 LinearLayout 而不是 ScrollView

2) 更好的选择是使用 ListView(这可能是使用 ScrollView 内的 LinearLayout 实现的)。

关于android - 一个ScrollView不能添加多个 View 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9660328/

相关文章:

android - 使用 ffmpeg 将 RTP/UDP 流转换为 HTTP

android - 在 CoordinatorLayout 中滚动时工具栏不会折叠

Java随机移动文件

android - 从图库到图像的图像以字节为单位..还有以字节为单位的数据..用于在android中上传facebook

找不到 Android 工作室 R.id.****

Android:Vertical LinearLayout - 两个 ImageView ,我希望底部的缩放为顶部的宽度

php - 开发动态 jQuery Mobile 移动应用程序的最佳方式是什么?

http - 如何通过http服务器强制刷新页面?

javascript - 如何在javascript中动态引用一个变量

android - 我怎样才能在 Android 中继续显示底部导航菜单标题?