使Listview占据整个屏幕

时间:2013-05-08 19:38:22

标签: java android listview android-listview

我有listview,我想填满整个屏幕,listview中有四个项目。填好四个项目后,它会在下面留下空白区域。您可以在屏幕截图中看到。它留下了空白。我希望覆盖整个屏幕。

enter image description here

我想这样:

enter image description here

这是源代码

MainActivity.java。

public class MainActivity extends Activity {
    ListView resultPane;
    List<Taskinfo> list;
    CustomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        resultPane = (ListView) findViewById(R.id.mylist);
        list = new ArrayList<Taskinfo>();
        Resources res = getResources(); // resource handle
        Drawable drawable = res.getDrawable(R.drawable.browse_home);

        list.add(new Taskinfo("Browse", drawable));
        drawable = res.getDrawable(R.drawable.jewelry);
        list.add(new Taskinfo("Whats New", drawable));
        drawable = res.getDrawable(R.drawable.show);
        list.add(new Taskinfo("Upcoming Show", drawable));
        drawable = res.getDrawable(R.drawable.contact);
        list.add(new Taskinfo("Contact Us", drawable));

        adapter = new CustomAdapter(this, list);
        resultPane.setAdapter(adapter);



    }

}

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
    private Context context;
    private List<Taskinfo> list;

    public CustomAdapter(Context context, List<Taskinfo> list) {
        this.context = context;
        this.list = list;

    }

    public View getView(int index, View view, final ViewGroup parent) {

        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.single_list_item, parent, false);
        }

        Taskinfo t = list.get(index);

        RelativeLayout l = (RelativeLayout) view
                .findViewById(R.id.testrelative);

        l.setBackgroundDrawable(t.getImage());

        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText(t.getName());

        return view;
    }

}

single_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/testrelative"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/browse_home"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!-- ListRow Left sied Thumbnail image -->

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"

        android:padding="3dip" >
    </LinearLayout>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp"
        android:text=""
        android:textColor="#040404"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" />

</RelativeLayout>

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45sp"
        android:id="@+id/llayout"
        android:background="@drawable/navbar"
        android:padding="3sp" >



        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="Golden Stone"

            android:textColor="@color/white"
            android:textSize="20sp" >
        </TextView>


    </LinearLayout>


   <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_below="@id/llayout"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

在这种情况下使用ListView是奇怪且不必要的。想想你的约束(比如:如果项目溢出会怎样?)并且只使用正确的布局管理器。像垂直LinearLayout一样,最后一个项目的布局权重非零。

ListView只有在你需要一个能够即时生成列表项的抽象时才有意义。

答案 1 :(得分:1)

您可能希望在ListView上设置背景。目前您的视图应该是底部,但ListView的背景是透明的,因此将其设置为白色应该可以实现您的要求。

<ListView ...
    android:background="@android:color/white"
... />

答案 2 :(得分:0)

如果项目很少,您可以改用LinearLayout,并为每个项目赋予权重。

但是,请注意android支持许多设备和屏幕,因此您可能希望限制每行的小小。

无论如何,如果你想让每一行都符合拟合高度,你可以检查它的大小,然后除以项目数。

为了获得listView的大小,你可以使用我已经制作的this small snippet

相关问题