3

もしそうなら、ScrollViewがかなり不自由であるか(疑わしい)、またはそれを行う他の方法があるようです。これが私のコードです。どこで爆撃するか (2 回目のループ、つまり) はコメントされています。

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();
        }
    }
}
4

3 に答える 3

11

これを解決するためにできることは 2 つあります。

1) の唯一の子をScrollView垂直にし、すべての子を の代わりに にLinearLayout追加します。LinearLayoutScrollView

2) 好ましい代替手段は、 a を使用することListViewです (これはおそらく a のLinearLayout内部を使用して実装されScrollViewます)。

于 2012-03-12T00:31:06.363 に答える
7

に含めることができるビューは 1 つだけScrollViewです。ただし、そのビューはLinearLayout. 通常、私が行うことは、そのようなビューを に追加することScrollViewであり、うまく機能します。

例:

<ScrollView>
  <LinearLayout>
    <ImageView/>
    <TextView/>
  </LinearLayout>
</ScrollView>
于 2012-03-12T00:27:48.460 に答える