如果是这样,似乎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();
}
}
}
答案 0 :(得分:9)
你可以做两件事来解决这个问题。
1)让ScrollView
的唯一孩子成为垂直LinearLayout
并将所有孩子添加到LinearLayout
而不是ScrollView
。
2)一个更好的选择是使用ListView
(可能使用LinearLayout
内的ScrollView
来实现。
答案 1 :(得分:6)
ScrollView
中只能包含一个视图。但是,该视图可以是布局,如LinearLayout
。通常我所做的是将这样的视图添加到ScrollView
,并且效果很好。
示例:
<ScrollView>
<LinearLayout>
<ImageView/>
<TextView/>
</LinearLayout>
</ScrollView>
答案 2 :(得分: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>
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" />