在Android中静态创建2X2图像视图的网格视图

时间:2012-06-23 16:02:02

标签: android gridview imageview

我正在尝试创建一个视图,其中包含2x2格式的4 imageView。这就是我的想法:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2">

    <ImageView 
        android:id="@+id/pic1" 
        android:layout_row="0"
        android:layout_column="0"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingRight="4dip" />
    <ImageView 
        android:id="@+id/pic2" 
        android:layout_row="1"
        android:layout_column="0"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <ImageView 
        android:id="@+id/pic3" 
        android:layout_row="0"
        android:layout_column="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <ImageView 
        android:id="@+id/pic4" 
        android:layout_row="1"
        android:layout_column="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</GridLayout>

这似乎不起作用,就像我跑

时一样
setContentView(R.layout.grid_view)

我收到此错误:

Binary XML file line #2: Error inflating class GridLayout

有谁知道为什么会这样?我试着按照http://android-developers.blogspot.co.il/2011/11/new-layout-widgets-space-and-gridlayout.html上的说明进行操作但是没有多大帮助

2 个答案:

答案 0 :(得分:0)

第5行的XML文件中存在错误

android:layout_height="match_parent""

属性值以""而不是"结尾。

答案 1 :(得分:0)

您应该使用网格视图,而是使用GridLayout。 试试这篇博文: http://www.java-samples.com/showtutorial.php?tutorialid=1518

那么你的R.id.grid_view在哪里?我没有在你的xml中看到像grid_view这样的名字。