滚动视图中的列表视图。我无法滚动列表,它只显示3个数据库文件

时间:2015-12-14 13:12:43

标签: android eclipse scrollview

请帮我滚动数据库列表。它只显示3个数据库文件,我无法滚动该数据库列表。请帮我解决这个问题。我是android的初学者。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/White"
    android:gravity="center|top"
    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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="information"
        android:textSize="22dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@color/DarkGray"
        android:gravity="center|top"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="information"
            android:textColor="@color/DarkMagenta"
            android:textSize="20dp" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="@color/Black" />

        <TextView
            android:id="@+id/backupView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select storage location"
            android:textSize="18dp" />

        <RadioGroup
            android:id="@+id/radioGroupBackup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/phoneBackupRb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Phone Memory" />

            <RadioButton
                android:id="@+id/externalBackupRb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="External Memory" />
        </RadioGroup>

        <Button
            android:id="@+id/backupBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:onClick="onClickBackup"
            android:text="Back Up" />

        <TextView
            android:id="@+id/backupSuccessView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:text=""
            android:textSize="15dp" />

        <Button
            android:id="@+id/AutobackupBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:onClick="onClickAutoBackup"
            android:text="Auto BackUp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        android:background="@color/DarkGray"
        android:gravity="center|top"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="information"
            android:textColor="@color/DarkMagenta"
            android:textSize="20dp" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="@color/Black" />

        <TextView
            android:id="@+id/restoreView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select storage location"
            android:textSize="18dp" />

        <RadioGroup
            android:id="@+id/radioGroupRestore"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/phoneRestoreRb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Phone Memory" />

            <RadioButton
                android:id="@+id/externalRestoreRb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="External Memory" />
        </RadioGroup>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="@color/Black" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="20dp"
            android:text="Select database file to restore"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/restoreResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:text=""
            android:textSize="15dp" />
    </LinearLayout>

    <ListView
            android:id="@+id/listDatabase"
            android:layout_width="fill_parent"
            android:layout_height="150dp" />
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

也许最好将ListView移动到其他Activity中,这将扩展ListActivity。然后它肯定是可滚动的。

答案 1 :(得分:0)

嗨默认情况下,listview无法在scrollview中添加。为此,您需要根据项目计算列表视图的大小,或者需要覆盖listview的onMeasure。

下面添加了带有overrid onMeasure方法的自定义Listview,使用此listview而不是默认值。还要确保在xml文件中高度应为 wrap_content

还可以将scrollview的 fillviewport 属性设置为 true ,以便使用全高。

 public class MyListView extends ListView {

public MyListView(Context context) {
    super(context);
}

public MyListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightSpec;

    if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {

        // The two leftmost bits in the height measure spec have
        // a special meaning, hence we can't use them to describe height.
        heightSpec = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >>2, MeasureSpec.AT_MOST);
    }
    else {
        // Any other height should be respected as is.
        heightSpec = heightMeasureSpec;
    }

    super.onMeasure(widthMeasureSpec, heightSpec);
}  
} 

答案 2 :(得分:0)

public void showListView(){

dbList = (ListView) findViewById(R.id.listDatabase);
ListAdapter listAdapter = dbList.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }
    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(
            dbList.getWidth(), MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, dbList);
        listItem.measure(desiredWidth,
                MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = dbList.getLayoutParams();
    params.height = totalHeight
            + (dbList.getDividerHeight() * (listAdapter
                    .getCount() - 1)) + 30;
    dbList.setLayoutParams(params);
    dbList.requestLayout();

}