Android-grid-view
Androidグリッドビュー
'_Android GridView は2次元のスクロールグリッド(行と列)にアイテムを表示し、グリッドアイテムは必ずしも事前に決定されているわけではありませんが、 ListAdapter を使用してレイアウトに自動的に挿入されます_
グリッドビュー
'_アダプタは、UIコンポーネントと、データをUIコンポーネントに入力するデータソースとの間を実際に橋渡しします。 アダプタを使用して、スピナー、リストビュー、グリッドビューなどにデータを提供できます。_
GridViewの属性
以下は、GridViewに固有の重要な属性です-
Sr.No | Attribute & Description |
---|---|
1 |
android:id これは、レイアウトを一意に識別するIDです。 |
2 |
android:columnWidth これは、各列の固定幅を指定します。 これは、px、dp、sp、in、またはmmになります。 |
3 |
android:gravity 各セル内の重力を指定します。 可能な値は、top、bottom、left、right、center、center_vertical、center_horizontalなどです。 |
4 |
android:horizontalSpacing 列間のデフォルトの水平方向の間隔を定義します。 これは、px、dp、sp、in、またはmmになります。 |
5 |
android:numColumns 表示する列の数を定義します。 「100」やauto_fitなどの整数値で、使用可能なスペースを埋めるためにできるだけ多くの列を表示することを意味します。 |
6 |
android:stretchMode 利用可能な空のスペースがある場合、それを埋めるために列がどのように伸びるかを定義します。 これは値のいずれかでなければなりません-
|
7 |
行間のデフォルトの垂直方向の間隔を定義します。 これは、px、dp、sp、in、またはmmになります。 |
例
この例では、GridViewを使用して独自のAndroidアプリケーションを作成する方法を示す簡単な手順を紹介します。 _Hello World Example_の章で作成したAndroidアプリケーションを変更するには、次の手順に従います-
Step | Description |
---|---|
1 | You will use Android studio IDE to create an Android application and name it as HelloWorld under a package com.example.helloworld as explained in the Hello World Example chapter. |
2 | Modify the detault content of res/layout/activity_main.xml file to include GridView content with the self explanatory attributes. |
3 | No need to change string.xml, Android studio takes care of defaults strings which are placed at string.xml |
4 | Let’s put few pictures in res/drawable-hdpi folder. I have put sample0.jpg, sample1.jpg, sample2.jpg, sample3.jpg, sample4.jpg, sample5.jpg, sample6.jpg and sample7.jpg. |
5 | Create a new class called ImageAdapter under a package com.example.helloworld that extends BaseAdapter. This class will implement functionality of an adapter to be used to fill the view. |
6 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
以下は、変更されたメインアクティビティファイル src/com.example.helloworld/MainActivity.java の内容です。 このファイルには、基本的な各ライフサイクルメソッドを含めることができます。
以下は res/layout/activity_main.xml ファイルの内容です-
以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-
以下は src/com.example.helloworld/ImageAdapter.java ファイルの内容です-
変更したばかりの* Hello World!アプリケーションを実行してみましょう。 環境設定中に *AVD を作成したと思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
サブアクティビティの例
選択したグリッド画像をフルスクリーンで表示する上記の例の機能を拡張しましょう。 これを実現するには、新しいアクティビティを導入する必要があります。 アクティビティクラスを実装し、そのアクティビティをAndroidManifest.xmlファイルで定義し、関連するレイアウトを定義し、最終的にそのサブアクティビティをメインのメインアクティビティにリンクするために必要なすべての手順を実行する必要があるアクティビティを念頭に置いてくださいアクティビティクラス。 したがって、上記の例を変更する手順に従ってみましょう-
Step | Description |
---|---|
1 | You will use Android studio IDE to create an Android application and name it as HelloWorld under a package com.example.helloworld as explained in the Hello World Example chapter. |
2 | Create a new Activity class as SingleViewActivity.java under a package com.example.helloworld as shown below. |
3 | Create new layout file for the new activity under res/layout/ folder. Let’s name this XML file as single_view.xml. |
4 | Define your new activity in AndroidManifest.xml file using <activity…/> tag. An application can have one or more activities without any restrictions. |
5 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
以下は、変更されたメインアクティビティファイル src/com.example.helloworld/MainActivity.java の内容です。 このファイルには、基本的なライフサイクルメソッドのそれぞれを含めることができます。
以下は、新しいアクティビティファイル src/com.example.helloworld/SingleViewActivity.java ファイルの内容です-
以下は res/layout/single_view.xml ファイルの内容です-
以下は、2つの新しい定数を定義する AndroidManifest.xml の内容です-
変更したばかりの* Hello World!アプリケーションを実行してみましょう。 環境設定中に *AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
これで、いずれかの画像をクリックすると、たとえば単一の画像として表示されます-
'_上記の画像は、Androidの公式Webサイトからのものです。_