如何强制GridView使用整个屏幕(不考虑显示大小)?

7
我有以下的布局文件,其中包含一个GridView和一个作为背景的ImageView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF">
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_marginRight="-70dp"
            android:layout_marginBottom="-50dp"
            android:src="@drawable/s_background"/>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent">
        <GridView xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/gridview"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:columnWidth="90dp"
                  android:numColumns="auto_fit"
                  android:verticalSpacing="10dp"
                  android:horizontalSpacing="10dp"
                  android:stretchMode="columnWidth"
                  android:gravity="center"/>
    </LinearLayout>
</FrameLayout>

这是我在网格中每个“单元格”中使用的实际项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
            android:id="@+id/cardInGrid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:singleLine="true"
            android:textSize="40sp"
            android:textColor="#660099"
            android:typeface="serif"/>
</LinearLayout>

我目前在我的设备上看到以下内容:

enter image description here

有没有办法让GridView中的每个项目变大,以适应屏幕大小,这样我就不会在显示器底部有未使用的白色空间了?
这在模拟器上运行良好,但在实际设备上,屏幕分辨率更高,因此出现了底部的白色空间。
非常感谢。

有趣的是,如果我将android:minSdkVersion设置为任何值,例如6/7/8,我会得到上述行为。一旦我完全删除它,网格视图就会填满更多的屏幕空间。有什么想法吗? - Jimmy
@parag:我该如何强制我的网格布局覆盖整个屏幕……请帮帮我? - Shahzad Imam
@ShahzadImam 在项目中设置背景图片。 - Parag Chauhan
@parag 整个网格视图的背景图片 - Shahzad Imam
@ShahzadImam 没有该项。 - Parag Chauhan
显示剩余2条评论
3个回答

16

这样做效果很好。不要忘记垂直间距。

public class MyAdapter extends BaseAdapter {
    public static int ROW_NUMBER = 5;

    public MyAdapter (Context mContext, ArrayList<String> list) {
        this.context = mContext;
        lstDate = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
        }
        // we need to decrease cell height to take into account verticalSpacing for GridView
        int cellHeight = StrictMath.max(parent.getHeight() / ROW_NUMBER - parent.getContext().getResources().getDimensionPixelOffset(R.dimen.grid_spacing), 320);
        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                parent.getHeight() / ROW_NUMBER);
        convertView.setLayoutParams(param);

        return convertView;
    }

1
如果适配器在视图出现之前有可用数据,则可能无法正常工作。第一个视图可能会在父级甚至没有高度之前被获取。此外,您不必在适配器中获取对“GridView”的引用:parent参数已经包含它了。 - Marc Plano-Lesay

3

不能自动完成。

特别是,您的单元格是文本。Android无法准确猜测文本应该有多大才能实现您的目标,特别是当您考虑到换行时。

GridView 的目的是在显示器底部留下“未使用的白色空间”,以便在没有足够数据填充屏幕时灵活处理多个屏幕大小,并且如果有更多数据,则可以容纳滚动。如果您想要类似于仪表板模式的东西,请考虑使用DashboardLayout或类似的内容。


1
DashboardLayout 重定向到 GitHub,而不是 Android!对我来说这是一个重要的细节... - Radu

0

你尝试过使用 android:layout_weight="1" 吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/gridview"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:columnWidth="90dp"
              android:numColumns="auto_fit"
              android:verticalSpacing="10dp"
              android:horizontalSpacing="10dp"
              android:stretchMode="columnWidth"
              android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>

或者

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
android:layout_weight="1">
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/gridview"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:columnWidth="90dp"
              android:numColumns="auto_fit"
              android:verticalSpacing="10dp"
              android:horizontalSpacing="10dp"
              android:stretchMode="columnWidth"
              android:gravity="center"
/>
</LinearLayout>

希望有所帮助?


我已经尝试过那些方法,不幸的是它们似乎都没有任何效果。:( - Jimmy
该死:(也许像Mark所说的那样,使用GridView是不可能的,标准的GridView只是设计用于从上到下显示项目,并在下面留有空白。) - wired00

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接