3

在我的应用程序中,我有一些按钮和一个具有不同布局的列表视图,并且列表视图位于最后一个位置。

我的问题是我附加了ScrollView作为根布局,因此滚动正确完成。但是ListView可见的只有一个列表项并且没有滚动。

布局:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/taskscrollviewid"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:scrollbars="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/txtdate"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector" />

            <Button
                android:id="@+id/btnTaskTimeid"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector" />
        </LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/txtItem"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/edittextselector"
                android:drawableEnd="@drawable/inbox"
                android:gravity="top|left"
                android:hint="Enter task"
                android:inputType="textMultiLine"
                android:maxLines="5"
                android:minLines="3"
                android:scrollbars="vertical"
                android:singleLine="false" />

            <ImageButton
                android:id="@+id/imagebuttonid"
                android:layout_width="31dp"
                android:layout_height="36dp"
                android:layout_gravity="right|bottom"
                android:background="@drawable/inbox" />
        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/btnaddcategoryid"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="AddCategory" />

            <Button
                android:id="@+id/btnaddseverityid"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="AddSeverity" />
        </LinearLayout>

        <Button
            android:id="@+id/btnadddb"
            android:layout_width="250px"
            android:layout_height="30px"
            android:layout_gravity="center|center_horizontal"
            android:layout_marginTop="10dp"
            android:background="@drawable/taskbuttonselector"
            android:text="Add This Task" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/fillbut"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="Remain Task" />

            <Button
                android:id="@+id/remainbut"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="Finish Task" />
        </LinearLayout>

        <ListView
            android:id="@+id/listid"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:clickable="true"
            android:orientation="vertical" >
        </ListView>
    </LinearLayout>

</ScrollView>
4

5 回答 5

2

添加

android:layout_marginLeft="10dp"
android:layout_height = "200dp"

给你的ListView

于 2013-07-08T12:21:27.903 回答
1

只需创建一个类

public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
    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);
}
}

然后在您的活动中,设置适配器后,请编写以下内容

Utility.setListViewHeightBasedOnChildren(yourListview);
于 2013-07-08T12:22:12.910 回答
0

ScrollView和之间存在冲突ListView。通常,您不能将 aListView放入 aScrollView中。ListView 已经了解滚动并将自己滚动。

几天前我也遇到了同样的问题。这是我的问题ScrollView 在 LinearLayout 中分布 ListView

在@David Wasser 的回答之后,我重新考虑了我的布局设计

于 2013-07-08T12:26:40.850 回答
0

试试这个-

 myList = (ListView) findViewById(R.id.listView);
              myList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
              Helper.getListViewSize(myList);

And-
public class Helper {
    public static void getListViewSize(ListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
      //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));
    }
}

你也可以参考这篇文章 -Androidhub4you-

于 2013-07-08T12:28:37.250 回答
0

最简单的解决方案..只给底部填充

<ListView
                android:id="@+id/lv_Jobs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:checkMark="?android:attr/listChoiceIndicatorMultiple"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:smoothScrollbar="true"
                 android:paddingBottom="50dp"
                android:textColor="#000000" />
于 2015-06-02T11:27:40.850 回答