Android-single-fragments

提供:Dev Guides
移動先:案内検索

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 の内容は次のとおりです-

package com.example.myfragments;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;

public class MainActivity extends Activity {

  /* *Called when the activity is first created.*/
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Configuration config = getResources().getConfiguration();

      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

     /**
 *Check the device orientation and act accordingly
     */

      if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        /**
 *Landscape mode of the device
        */
         LM_Fragement ls_fragment = new LM_Fragement();
         fragmentTransaction.replace(android.R.id.content, ls_fragment);
      }else{
        /**
 *Portrait mode of the device
        */
         PM_Fragement pm_fragment = new PM_Fragement();
         fragmentTransaction.replace(android.R.id.content, pm_fragment);
      }
      fragmentTransaction.commit();
   }

}

2つのフラグメントファイル LM_Fragement.java および PM_Fragment.java を作成します。

以下は LM_Fragement.java ファイルの内容です-

package com.example.myfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
   * Created by finddevguides7 on 8/23/2016.
*/

public class LM_Fragement extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     /**
 *Inflate the layout for this fragment
     */
      return inflater.inflate(R.layout.lm_fragment, container, false);
   }
}

以下は PM_Fragement.java ファイルの内容です-

package com.example.myfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
   * Created by finddevguides7 on 8/23/2016.
*/

public class PM_Fragement extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     /**
 *Inflate the layout for this fragment
     */
      return inflater.inflate(R.layout.pm_fragment, container, false);
   }
}

_res/layout_ディレクトリの下に2つのレイアウトファイル lm_fragement.xml および pm_fragment.xml を作成します。

以下は lm_fragement.xml ファイルの内容です-

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#7bae16">

   <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/landscape_message"
      android:textColor="#000000"
      android:textSize="20px"/>

<!-- More GUI components go here  -->

</LinearLayout>

以下は pm_fragment.xml ファイルの内容です-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#666666">

   <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/portrait_message"
      android:textColor="#000000"
      android:textSize="20px"/>

<!-- More GUI components go here  -->

</LinearLayout>

以下は、フラグメントを含む res/layout/activity_main.xml ファイルの内容です-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal">

   <fragment
      android:name="com.example.fragments"
      android:id="@+id/lm_fragment"
      android:layout_weight="1"
      android:layout_width="0dp"
      android:layout_height="match_parent"/>

   <fragment
      android:name="com.example.fragments"
      android:id="@+id/pm_fragment"
      android:layout_weight="2"
      android:layout_width="0dp"
      android:layout_height="match_parent"/>

</LinearLayout>
*res/values/strings.xml* ファイルの次の内容があることを確認してください-
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">My Application</string>
   <string name="landscape_message">This is Landscape mode fragment</string>
   <string name="portrait_message">This is Portrait mode fragment></string>
</resources>

作成したばかりの修正済み 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コンポーネントを使用できます。