Android-single-fragments
Android-単一のフラグメント
シングルフレームフラグメント
'_シングルフレームフラグメントは、ハンドホールドデバイス(モバイル)などの小さな画面デバイス用に設計されており、Android 3.0バージョンより上にある必要があります。_
例
この例では、独自の_Fragments_を作成する方法を説明します。 ここでは2つのフラグメントを作成し、デバイスの1つが横モードのときに使用し、もう1つのフラグメントをポートレートモードの場合に使用します。 したがって、_Hello World Example_を作成する際に行った手順と同様に、次の手順に従ってみましょう-
Step | Description |
---|---|
1 | You will use Android StudioIDE to create an Android application and name it as MyFragments under a package com.example.myfragments, with blank Activity. |
2 | Modify main activity file MainActivity.java as shown below in the code. Here we will check orientation of the device and accordingly we will switch between different fragments. |
3 | Create a two java files PM_Fragment.java and LM_Fragement.java under the package com.example.myfragments to define your fragments and associated methods. |
4 | Create layouts files res/layout/lm_fragment.xml and res/layout/pm_fragment.xml and define your layouts for both the fragments. |
5 | Modify the default content of res/layout/activity_main.xml file to include both the fragments. |
6 | Define required constants in res/values/strings.xml file |
7 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
変更されたメインアクティビティファイル MainActivity.java の内容は次のとおりです-
2つのフラグメントファイル LM_Fragement.java および PM_Fragment.java を作成します。
以下は LM_Fragement.java ファイルの内容です-
以下は PM_Fragement.java ファイルの内容です-
_res/layout_ディレクトリの下に2つのレイアウトファイル lm_fragement.xml および pm_fragment.xml を作成します。
以下は lm_fragement.xml ファイルの内容です-
以下は pm_fragment.xml ファイルの内容です-
以下は、フラグメントを含む res/layout/activity_main.xml ファイルの内容です-
作成したばかりの修正済み MyFragments アプリケーションを実行してみましょう。 環境のセットアップ中に AVD を作成したと思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。 Android StudioはアプリをAVDにインストールして起動します。セットアップとアプリケーションで問題がなければ、[エミュレータ]ウィンドウが表示され、[メニュー]ボタンをクリックして次のウィンドウを表示します。 それはあなたのコンピュータの速度に基づいて時間がかかる場合がありますので、我慢してください-
Android Portrait Fragment Demo
エミュレータ画面のモードを変更するには、次のことをしましょう-
- Macで fn + control + F11 を押して、風景をポートレートに、またはその逆に変更します。
- Windowsでは ctrl + F11 。
- ctrl + F11 on Linux。
モードを変更すると、次のようにランドスケープモードに実装したGUIを表示できます-
Android Landscape Fragment Demo
このようにして、同じアクティビティを使用できますが、異なるフラグメントを通じて異なるGUIを使用できます。 要件に基づいて、さまざまなGUIにさまざまなタイプのGUIコンポーネントを使用できます。