滚动在ScrollView中不起作用

时间:2014-03-09 17:15:12

标签: android

动态地我在滚动视图中添加了一些复选框。它工作正常,直到没有条目更少,但当复选框的数量更多时,它会离开显示器,我无法向下滚动。

  

MainActivity.java

private void displayContact() {
            // TODO Auto-generated method stub
        ScrollView parent_layout=(ScrollView)findViewById(R.id.r1);
        LinearLayout contact_layout=new LinearLayout(MainActivity.this);

        ContentResolver cr=getContentResolver();
        Cursor cur=cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if(cur.getCount()>0){
            //Toast.makeText(this, "hi", Toast.LENGTH_LONG).show();
            int tm=0;
            while(cur.moveToNext()){
                String id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if(Integer.parseInt(cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))>0){
                        Cursor pCur=cr.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?",
                                new String[]{id}, 
                                null);
                        while(pCur.moveToNext()){
                            String phoneNo=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            CheckBox c1 = new CheckBox (MainActivity.this);
                            c1.setText(name);
                            LayoutParams lp=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
                            c1.setLayoutParams(lp);
                            contact_layout.addView(c1);


                            //Toast.makeText(this, ""+name+","+phoneNo ,Toast.LENGTH_LONG).show();
                        }
                }
            }
        }


        parent_layout.addView(contact_layout);
    }

activity_main.xml中

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/r1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:scrollbarStyle="insideInset"
    android:scrollbars="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Button" />

</ScrollView>

如何启用滚动功能?

2 个答案:

答案 0 :(得分:1)

使用一个直接孩子定义您的Scrollview。

答案 1 :(得分:0)

您必须将复选框放在由ScrollView托管的一个LinearLayout中。

见这里:ScrollView dos not work