是否无法向ScrollView添加多个视图?

B. *_*non 3 android dynamic android-widget android-layout 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

mar*_*ych 10

你可以做两件事来解决这个问题.

1)让唯一的孩子ScrollView成为一个垂直LinearLayout的孩子,并将所有的孩子加到LinearLayout而不是ScrollView.

2)一个更好的选择是使用a ListView(可能使用a的LinearLayout内部实现ScrollView).


Pea*_*oto 6

一个视图中只能包含一个视图ScrollView.但是,该视图可以是布局,如a LinearLayout.通常我所做的就是为a添加这样的视图ScrollView,并且效果很好.

例:

<ScrollView>
  <LinearLayout>
    <ImageView/>
    <TextView/>
  </LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)