我在使 ListView 可滚动时遇到问题。我知道 listview 本身是可滚动的。但我必须放入..请告诉我如何使 listView 可滚动....非常感谢
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:focusable="false"
android:focusableInTouchMode="true"
android:layout_height="597dp"
android:orientation="vertical" >
<TextView
android:id="@+id/TextViewMob"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/textViewCNIC"
android:layout_below="@+id/textViewCNIC"
android:layout_marginTop="20dp"
android:text="Customer Mobile No."
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/TextViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/TextViewMob"
android:layout_below="@+id/editTextMob"
android:layout_marginTop="20dp"
android:text="Customer Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/TextViewAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/TextViewName"
android:layout_below="@+id/TextViewName"
android:layout_marginTop="20dp"
android:text="Mailing Address"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/EditTextAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/TextViewAdd"
android:layout_marginTop="20dp"
android:layout_toRightOf="@+id/TextViewAdd"
android:ems="10"
android:inputType="textMultiLine" />
<TextView
android:id="@+id/TextViewKin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/TextViewAdd"
android:layout_marginTop="20dp"
android:layout_toLeftOf="@+id/EditTextAdd"
android:text="Customer's Next of Kin"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/EditTextKin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/TextViewKin"
android:layout_toRightOf="@+id/TextViewKin"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/EditTextAdd"
android:layout_marginTop="20dp"
android:layout_toRightOf="@+id/TextViewName"
android:ems="10" >
</EditText>
<TextView
android:id="@+id/textViewPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/TextViewKin"
android:layout_marginTop="20dp"
android:layout_toLeftOf="@+id/EditTextKin"
android:text="Attach File"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/buttonCamera"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textViewPhoto"
android:layout_alignBottom="@+id/textViewPhoto"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="From Camera" />
<Button
android:id="@+id/buttonAttach"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonCamera"
android:layout_alignBottom="@+id/buttonCamera"
android:layout_marginLeft="18dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@+id/buttonCamera"
android:text="From Sdcard" />
<TextView
android:id="@+id/textViewCNIC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="65dp"
android:layout_toLeftOf="@+id/buttonCamera"
android:text="Customer CNIC"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/EditTextCNIC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textViewCNIC"
android:layout_alignBottom="@+id/textViewCNIC"
android:layout_toRightOf="@+id/textViewCNIC"
android:ems="10"
android:inputType="number" >
</EditText>
<EditText
android:id="@+id/editTextMob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/EditTextCNIC"
android:layout_alignTop="@+id/TextViewMob"
android:ems="10"
android:hint="Enter Ufone No. only"
android:inputType="phone" />
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/EditTextKin"
android:layout_marginBottom="43dp"
android:layout_marginRight="26dp"
android:text="Save and Open Account" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="19dp"
android:src="@drawable/cr_l0_button" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/EditTextKin"
android:layout_alignParentLeft="true">
</ListView>
</RelativeLayout>
</ScrollView>
我也试过这个:(可能有问题)
setListViewHeightBasedOnChildren(listviewattachment);//method call inonCreate
private void setListViewHeightBasedOnChildren(ListView listView) {
// TODO Auto-generated method stub
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}