2012-06-23 36 views
2

我試圖創建一個視圖,其中包括4個imageView以2x2形式。這是我腦子裏想的:在Android中靜態創建2X2圖片視圖的gridview

<?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 

有誰知道爲什麼這是怎麼回事?我tryed跟隨上http://android-developers.blogspot.co.il/2011/11/new-layout-widgets-space-and-gridlayout.html指示,但它並沒有多大幫助

回答

0

有第5行

android:layout_height="match_parent"" 

在XML文件中的錯誤的屬性值與""而不是"結束。

+0

是的,只是一個小小的錯字。當然,這不是造成這種錯誤的原因。我修正了它 –

+0

我看到了,我以爲你複製粘貼了你的代碼。 –

+0

如果你已經修復它然後分享它@n00bprogrammer –

0

你應該使用網格視圖,而是使用GridLayout。 試試這個博客帖子: http://www.java-samples.com/showtutorial.php?tutorialid=1518

到哪裏是你R.id.grid_view的方式?我沒有在你的xml中看到任何像grid_view這樣的名字。

+0

grid_view是XML文件的名稱。博客文章討論了動態使用網格,而我想靜態地使用網格,就像http://android-developers.blogspot.co.il/2011/11/new-layout-widgets-space-and-gridlayout.html –

+0

你不能像這樣加載一個xml文件。您只能以這種方式在xml中加載特定組件。試試這個 - 'setContentView(R.layout.gridLayout1)'這應該使網格充滿膨脹視圖。 –

+0

這是不正確的。我一直在其他項目中這樣做。 –