3

如果是这样,似乎 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();
        }
    }
}
4

3 回答 3

11

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

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

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

于 2012-03-12T00:31:06.363 回答
7

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

例子:

<ScrollView>
  <LinearLayout>
    <ImageView/>
    <TextView/>
  </LinearLayout>
</ScrollView>
于 2012-03-12T00:27:48.460 回答
1

如果您想添加更多嵌套滚动视图作为子视图仅包含一个直接视图,那么您必须仅包含一个视图子视图作为视图组,如线性布局,它可以容纳更多视图,因为您可以添加按钮等。换句话说因此,如果您将它们作为 NestedScrollView 的子视图,视图组可以容纳多个视图。我包含一个布局作为 NestedScrollView 的子级,并且它还有很多视图。

 <android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/descriptioncontent_layout" />

</android.support.v4.widget.NestedScrollView>

将多个项目添加到描述 content_layout ...等。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    card_view:cardCornerRadius="5dp">

    <include layout="@layout/descption_layout" />
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    card_view:cardCornerRadius="5dp">

    <include layout="@layout/howtodoit" />
</android.support.v7.widget.CardView>

说明_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Full Body:"
    android:id="@+id/description"
    android:textSize="20sp"
    android:textColor="@color/how"
    android:padding="10dp"
    android:layout_marginLeft="10dp"
    android:layout_gravity="center|left" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="If you are brand new to yoga, there are certain postures that are essential for you to learn so you can feel comfortable in a class or practicing on your own at home"
    android:id="@+id/detaildescription"
    android:textSize="18sp"
    android:padding="10dp"
    android:layout_gravity="center_horizontal" />

于 2018-03-12T06:25:33.303 回答