具有固定宽度和响应高度的网格视图(Android)

时间:2016-05-15 08:20:49

标签: android css gridview

我正在开发一个Android应用程序,我必须创建一个由网格视图组成的布局,其中两列具有固定宽度和一定数量的行。

这个gridview是由图像组成的,在我的网格的每个“正方形”中都有一个图像,我需要的尺寸正好是屏幕的大小,我不想通过滚动来获得视觉效果。

我能够确定长度,但不能确定高度。如何确保我的烤架具有我指定的尺寸?

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="am.MainActivity"
    tools:showIn="@layout/activity_main">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="2"
        android:verticalSpacing="4dp"
        android:horizontalSpacing="8dp"
        android:stretchMode="columnWidth"/>
</RelativeLayout>

这是java代码:

GridView gridview = (GridView) findViewById(R.id.gridview);
            gridview.setAdapter(new ImageAdapter(this));


  public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {

                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                int width = dm.widthPixels / 2;

                imageView = new ImageView(mContext);
                imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                imageView.setLayoutParams(new GridView.LayoutParams(width, width));

            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.bacheca, R.drawable.turni,
                R.drawable.utility, R.drawable.contatti,
                R.drawable.invia, R.drawable.info
        };
    }

输出完全适合宽度(屏幕尺寸中包含的两个图像),但高度错误,因为当我宁愿在屏幕上显示所有内容时,它会激活滚动

2 个答案:

答案 0 :(得分:1)

正如答案中所述 - Gridview with two columns and auto resized images

您可以为此

制作自定义ImageView
<package_that_contains_SquareImageView.SquareImageView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"/>

然后在网格视图单元格布局中使用它

package com.ecust.ecusthelper.util.network.httpurlconnection;

import android.util.Xml;

import org.junit.Assert;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;

public class XmlTest {

    @Test
    public void testXmlNonNull() {
        XmlPullParser pull = Xml.newPullParser();
        if (pull == null)
            Assert.fail("Why here is null");
    }
}

答案 1 :(得分:0)

如果您希望屏幕上显示所有网格的行,为什么要使用这行代码?

imageView.setLayoutParams(new GridView.LayoutParams(width, width));

它会使imageView具有方形的形状。所以它不适合你的屏幕高度!!

请测量屏幕高度(与测量宽度相同),然后将其设置为LayoutParams:

imageView.setLayoutParams(new GridView.LayoutParams(screenWidth/numOfColumn, screenHeight/numOfRow));
相关问题