【问题标题】:Fit GridView exactly into Screen使 GridView 完全适合屏幕
【发布时间】:2016-09-01 17:03:36
【问题描述】:

我已经阅读了一些关于此的帖子,但无法找出问题所在。我正在尝试在设备屏幕上完全适合 2 列中的 6 个图块的 GridView,这样就不需要滚动。这是它的样子:

所以我尝试将每个图块的高度计算为屏幕大小减去操作栏高度减去垂直空间。这是我计算瓷砖高度的代码:

public static int getTileHeight(int numOfRows, int verticalSpacing, Context context) {
    TypedValue tv = new TypedValue();
    int actionBarHeight = 0;
    if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
    }

    DisplayMetrics displaymetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;

    int px = Math.round((numOfRows - 1) * verticalSpacing * (context.getResources().getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return ((height - actionBarHeight - px) / numOfRows);
}

垂直间距由

检索
  gridView.getVerticalSpacing()

然后在我的 GridViewAdapter 中,我在 getView 中设置每个图块的高度:

  picture.getLayoutParams().height = getTileHeight(...);

其中图片是一个带有图片的 ImageView。

但是,GridView 的高度对于屏幕来说总是有点太大。我无法完美匹配屏幕高度。有什么我想念的想法吗?

【问题讨论】:

  • 你能分享你的xml吗?
  • 你总是正好有 6 个项目吗?如果是这样,也许GridView 不是最佳选择,您应该只使用一些LinearLayouts 而不是相等的layout_weights。

标签: android gridview


【解决方案1】:

你可以试试这个吗?看来您必须将操作栏大小加倍。

已编辑:

public static int getTileHeight(int numOfRows, int verticalSpacing, Context context) {
    TypedValue tv = new TypedValue();
    int actionBarHeight = 0;
    if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
    }
    int statusBarHeight = 0;
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
    }

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int height = size.y - actionBarHeight - statusBarHeight; // removed *2 for action bar height and added status bar height
    int totalSpacing = verticalSpacing*(numOfRows+1);

    return ((height - totalSpacing) / numOfRows); 
}

如果您需要顶部和底部间距,此代码将为您提供高度。我相信在总间距中使用 numberOfRows-1 将为您提供没有顶部和底部间距的瓷砖高度

【讨论】:

  • 不,这不起作用。使用您的代码,图块太小,导致底部有一些空闲空间。
  • 现在可以试试吗?我编辑了代码,在其中添加了状态栏高度,并删除了操作栏高度的 *2。我现在尝试了这段代码,它似乎正在工作。
  • 比以前好多了,但是底部还是有2mm左右的空白空间(在HTC One设备上)。
  • 我不确定,我在 nexus 4/5/S 上试过这个,他们在点上工作。你只是添加状态栏高度吗?您是否删除了操作栏上的 *2? (如果您的应用程序在状态栏下(如视图寻呼机)中有标签,则需要 *2)
【解决方案2】:

感谢 Karakuri 的评论,我将 GridView 更改为 LinearLayout,因为我总是有 6 个图块。我现在正在使用这个 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".333">
    <include android:id="@+id/burnt_tile"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        layout="@layout/tile"/>
    <include android:id="@+id/meal_tile"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        layout="@layout/tile"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".333">
    <include android:id="@+id/peace_tile"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        layout="@layout/tile"/>
    <include android:id="@+id/sin_tile"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        layout="@layout/tile"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".333">
    <include android:id="@+id/trespass_tile"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        layout="@layout/tile"/>
    <include android:id="@+id/overview_tile"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        layout="@layout/tile"/>
</LinearLayout>
</LinearLayout>

这会按照我想要的方式对齐所有图块! @Karakuri:谢谢!

【讨论】:

    猜你喜欢
    • 2016-05-25
    • 1970-01-01
    • 2019-08-04
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    相关资源
    最近更新 更多