【问题标题】:How can I force a GridView to use the whole screen (regardless of display size)?如何强制 GridView 使用整个屏幕(无论显示大小)?
【发布时间】:2023-03-22 11:55:01
【问题描述】:

我有以下布局文件,它后面有一个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>

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

有没有什么办法可以让 GridView 中的每个项目都变大,使其适合屏幕的大小,并且我在显示屏底部没有未使用的空白?

这在模拟器上运行良好,但在设备上屏幕分辨率更高,因此底部有空白。

非常感谢

【问题讨论】:

  • 有趣的是,如果我将 android:minSdkVersion 设置为任何值,例如 6/7/8,我就会得到上述行为。在我完全删除它的那一刻,gridview 会填满更多的屏幕空间。有什么想法吗?
  • Esley 你有关于 Gridview 的任何解决方案吗?我也面临同样的问题...
  • @parag : 你能帮我解决这个问题吗........
  • @parag : 如何强制我的网格布局覆盖整个屏幕.....请帮帮我?
  • @ShahzadImam 在项目中设置背景图片

标签: android gridview android-2.1-eclair android-2.0-eclair


【解决方案1】:

这会很好用。不要忘记垂直间距。

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;
    }

【讨论】:

  • 如果适配器在视图出现之前有可用的数据,它可能无法工作。第一个视图可能在父级甚至有高度之前被获取。此外,您不必在适配器中获取对 GridView 的引用:parent 参数已经包含它。
【解决方案2】:

不是自动的。

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

GridView的要点是“显示器底部未使用的空白”,如果你没有足够的数据填满屏幕,那么它可以灵活处理多种屏幕尺寸,也可以容纳如果有更多数据,则滚动。如果您的目标是类似于仪表板模式的东西,请考虑使用DashboardLayout 或类似的东西。

【讨论】:

  • DashboardLayout 重定向到 GitHub,而不是 Android!这是一个重要的细节,至少对我来说......
【解决方案3】:

你试过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>

希望有帮助吗?

【讨论】:

  • 我试过了,不幸的是它们似乎都没有任何效果。 :(
  • 该死的:(也许它只是不可能使用像标记这样的网格视图,标准的网格视图只是设计为从上到下显示项目,下面有空格
猜你喜欢
  • 2014-12-27
  • 2016-05-17
  • 2018-02-05
  • 2012-01-31
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
相关资源
最近更新 更多