简体   繁体   English

用 Gridview 项目填充整个屏幕

[英]Filling the whole screen with Gridview items

I have a GridView (with 2 columns and 2 rows) and want to stretch the rows to fill the whole screen.我有一个 GridView(有 2 列和 2 行)并且想要拉伸行以填充整个屏幕。 It should look the same on all devices.它应该在所有设备上看起来都一样。

在此处输入图片说明

That means I don't want some empty space under the last row.这意味着我不想在最后一行下面有一些空白空间。 So I searched a little bit and found a solution to set the minimum height of the GridView dynamically with following code:所以我搜索了一下,找到了一个解决方案,可以使用以下代码动态设置 GridView 的最小高度:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int width = metrics.widthPixels;
int height = metrics.heightPixels;

In addition I added these lines of code in my Adapter:此外,我在我的适配器中添加了这些代码行:

gridView = inflater.inflate(R.layout.act_main_menu_sub, null);
gridView.setMinimumHeight(ActMainMenu.height/2);

But now it looks like this:但现在看起来像这样:

在此处输入图片说明

The View is scrollable now and the items have the same size.视图现在可以滚动并且项目具有相同的大小。 But it doesn't fit on the screen.但它不适合屏幕。

Here is the code of my layout including the GridView:这是我的布局代码,包括 GridView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/layout_main_menu"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#b2b2b2"
          android:orientation="vertical">

<!-- android:layout_height="wrap_content" -->
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:padding="2dp"
    app:titleMarginStart="20dp"
    app:titleTextAppearance="@style/MyMaterialTheme.Base.TitleTextStyle"
    app:titleTextColor="@color/textColorPrimary">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="MASTERFITNESS TRAINERSCHULUNG"
        android:textColor="@android:color/white"
        android:textStyle="bold|italic"/>

</android.support.v7.widget.Toolbar>


<!--android:columnWidth="160dp"-->
<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:gravity="center"
    android:horizontalSpacing="20dp"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp"></GridView>

</LinearLayout>

What is the problem here?这里有什么问题?

The problem with your 2nd screen shots in the height calculation.高度计算中第二个屏幕截图的问题。 metrics.heightPixels gives you the full height of the screen, including the toolbar and any margins you have. metrics.heightPixels为您提供屏幕的完整高度,包括工具栏和您拥有的任何边距。 These should 1st be subtracted out, then divide the screen in half.这些应该首先被减去,然后将屏幕分成两半。

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    // your layout has 10dp margin (top & bottom) + 20 dp between grid items, 10+10+20=40
    int marginSizeDp = 40;
    float scale = metrics.density;
    int marginSizePx = Math.round (marginSizeDp * scale);

    int appBarHeight = getAppBarHeight();

    int height = metrics.heightPixels;
    int minHt = Math.round((height - marginSizePx - appBarHeight)/2);
    gridView.setMinimumHeight(minHt);

Get the toolbar height thanks to AZ13 at https://stackoverflow.com/a/7167086/7292819借助 AZ13 在https://stackoverflow.com/a/7167086/7292819 上获取工具栏高度

    // returns height of app bar in pixels
    private int getAppBarHeight() {
        final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
                new int[] { android.R.attr.actionBarSize });
        int appBarHeight = (int) styledAttributes.getDimension(0, 0);
        styledAttributes.recycle();

        return appBarHeight;
    }

Truthfully, if this layout is static (there's only 4 items, no scrolling, little or no changing of the content of each item), I probably wouldn't bother with a GridView & adapter.说实话,如果这个布局是静态的(只有 4 个项目,没有滚动,每个项目的内容几乎没有变化),我可能不会打扰 GridView 和适配器。 A RelativeLayout or GridLayout might be better.相对布局或网格布局可能更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM