讓我們打破這一點。 首先你聲明你想建立一個Grid視圖。爲了簡單起見,網格視圖應該是activity.xml文件的根佈局。在這裏,您將使用android:numColumns屬性設置列數。這裏是什麼看起來像一個示例代碼:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:numColumns="2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.app_name.activity"/>
接下來,您將需要一個.java文件打造的「源」,這將在此列表視圖。您可以通過使用ArrayList的<>在java文件來實現這個目的,找到該GridView的視圖組用下面的代碼
GridView gridview = (GridView) findViewById(R.id.list);
現在你可以使用建立一個ArrayList源。
ArrayList<YourElement> array = new ArrayList<YourElement>();
將項目添加到這個數組列表中的使用。新增方法
array.add();
我們數組列表連接到網格視圖您將使用.setAdapter方法並傳入的名稱數組,在這種情況下是數組。
gridview.setAdapter(array);
這是盡我所能帶你。希望這可以幫助其他人