【问题标题】:Is it impossible to add multiple views to a ScrollView?是否不可能将多个视图添加到 ScrollView?
【发布时间】:2012-03-12 00:25:01
【问题描述】:

如果是这样,那么 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();
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

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

    1) 将ScrollView 的唯一子代设为垂直LinearLayout,并将所有子代添加到LinearLayout 而不是ScrollView

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

    【讨论】:

    • 感谢您的帮助。我会选择选项 A,因为选项 2(这是我的第一次尝试)的问题是我无法找到一种能够在运行时以编程方式识别每个复选框的方法 - 如何知道哪个复选框在点击了哪一行。我需要复选框具有与联系人 ID 对应的标记值,以便我知道为每个联系人选择了哪些选项。这就是我采用动态创建小部件路线的原因,因此我可以在创建复选框(和 textView)小部件的循环中添加该标签。
    【解决方案2】:

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

    例子:

    <ScrollView>
      <LinearLayout>
        <ImageView/>
        <TextView/>
      </LinearLayout>
    </ScrollView>
    

    【讨论】:

    • 以防万一他还是一头雾水。 LinearLayout 直接放在 ScrollView 内。它只是用来包装你想放在那里的所有东西。
    • 我想我将 LinearLayout 放在 ScrollView 中;我想我需要随后创建包含三个复选框和 textView 的 LinearLayout,并将它们添加到最外面的 LinearLayout,而不是 ScrollView。
    • 9 年后仍然如此 ?
    【解决方案3】:

    如果您想添加更多嵌套滚动视图作为子视图仅包含一个直接视图,那么您必须仅包含一个视图子视图作为视图组,如线性布局,它可以容纳更多视图,因为您可以添加按钮等。换句话说,如果您将它们作为 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>
    

    Description_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" />
    

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多